Android4.4 4.2keyguard锁屏流程梳理

Android4.4 锁屏流程梳理

刚毕业不久由于项目需要就接触到锁屏,从2.2到4.1都解过bug,也定制过一些功能。4.1之前的锁屏工作不难,但很费时间,因为它的逻辑,视图,资源分别分布在不同的路径下,就像散落在海边沙滩上的珠子,想串起来还是蛮费劲的。最开始时锁屏就是改个字段也要全编译生成img。后来新技能get,会针对修改的地方进行单编译,但每次编译jar,导入手机,重启看效果也是不方便的。

一年前把锁屏交出去就没有再看过了,前些日子自己的谷歌四太子升到4.4,发现锁屏有很大变化,可以左右滑页,添加删除widget,添加删除分页。简直就是一个简化版的launcher。很好奇google是怎么改变的,于是费了很大的劲拉了google的最新源码。在android 4.4中这个模块的改动简直是巨大,这里略作整理。

1.文件目录:

a,锁屏代理是在Frameworks/base/policy/src/com/android/internal/policy/impl/keyguard下:


b,整个工程应用在framework/package下,结构和功能现在都和 systemUI类似:


c,keyguard的对外接口Frameworks/base/core/java/android/app/keyguardManager.java:

android4.2前做一些第三方锁屏软件都会用到该服务接口来控制系统锁屏(比如禁止系统锁屏),现在该接口已经不建议使用了,有更好的做法:

/**
     * @deprecated使用{@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
     * 来代替; 利用该方式可以使应用达到禁用锁屏的效果而不需要额外的权限申请。this allows you to
     * Enables you to lock or unlock thekeyboard. Get an instance of this class by
     * calling {@link android.content.Context#getSystemService(java.lang.String)Context.getSystemService()}.
     * This class is wrapped by {@link android.app.KeyguardManagerKeyguardManager}.
     * @param tag A tag that informallyidentifies who you are (for debugging who
     *  is disabling he keyguard).
     *
     * @return A {@link KeyguardLock} handle to use todisable and reenable the
     *   keyguard.
     */
    @Deprecated
    public KeyguardLock newKeyguardLock(Stringtag) {
        return new KeyguardLock(tag);
    }

Keyguard锁屏流程图


Keyguard锁屏view层次图:

 

Keyguard锁屏重要类分析:

1.PhoneWindowManager.java

这个类很厉害也很重要,它由WindowManagerService派生,处理了phone的顶层逻辑,主要有以下几块:

a)      横竖屏处理(屏幕旋转等)

publicvoidsetCurrentOrientationLw(intnewOrientation){
synchronized (mLock){
if (newOrientation != mCurrentAppOrientation) {
mCurrentAppOrientation = newOrientation;
updateOrientationListenerLp();
            }
        }
    }

b)      是否显示状态条或者navigation_bar。

privateintupdateSystemUiVisibilityLw() {
// If there is no window focused,there will be nobody to handle the events
// anyway, so just hang on inwhatever state we're in until things settle down.
WindowState win = mFocusedWindow != null ? mFocusedWindow :mTop
  • 20
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
非常好的问题!实现一个锁屏背单词的应用程序需要使用到以下技术: 1. Android广播机制:使用BroadcastReceiver接收系统锁屏事件。 2. Android服务机制:使用Service来后台运行应用程序并提供背单词功能。 3. Android界面开发:使用Android的布局和控件来构建应用程序的界面。 下面是一个简单的实现锁屏背单词的应用程序的步骤: 1. 创建一个BroadcastReceiver类来接收系统锁屏事件。在这个类中,你可以启动一个Service来后台运行应用程序并提供背单词功能。例如: ```java public class LockScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // 启动后台服务 Intent serviceIntent = new Intent(context, WordService.class); context.startService(serviceIntent); } } } ``` 2. 创建一个Service类来提供背单词功能。在这个类中,你可以使用一个定时器来定时显示单词,并在屏幕上显示它。例如: ```java public class WordService extends Service { private Timer timer; private String[] words = {"apple", "banana", "cherry", "orange"}; @Override public void onCreate() { super.onCreate(); // 启动定时器 timer = new Timer(); timer.schedule(new ShowWordTask(), 0, 5000); } @Override public void onDestroy() { super.onDestroy(); // 停止定时器 timer.cancel(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private class ShowWordTask extends TimerTask { @Override public void run() { // 随机选择一个单词 Random random = new Random(); int index = random.nextInt(words.length); String word = words[index]; // 在屏幕上显示单词 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, PixelFormat.TRANSPARENT); TextView textView = new TextView(WordService.this); textView.setText(word); textView.setGravity(Gravity.CENTER); textView.setTextColor(Color.WHITE); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 48); windowManager.addView(textView, layoutParams); // 定时关闭单词 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } windowManager.removeView(textView); } } } ``` 3. 创建一个Activity类来作为锁屏界面。在这个类中,你可以设置背景图片和显示单词的TextView。例如: ```java public class LockScreenActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lock_screen); // 设置背景图片 ImageView imageView = findViewById(R.id.lock_screen_bg); imageView.setImageResource(R.drawable.lock_screen_bg); // 显示单词 TextView textView = findViewById(R.id.lock_screen_word); textView.setText(getIntent().getStringExtra("word")); } } ``` 4. 创建一个布局文件来定义锁屏界面的布局。例如: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/lock_screen_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/lock_screen_bg" /> <TextView android:id="@+id/lock_screen_word" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="@android:color/white" android:textSize="72sp" /> </RelativeLayout> ``` 5. 在AndroidManifest.xml文件中注册BroadcastReceiver和Service,并声明android.permission.DISABLE_KEYGUARD权限。例如: ```xml <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <application ...> <receiver android:name=".LockScreenReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.SCREEN_OFF" /> </intent-filter> </receiver> <service android:name=".WordService" android:enabled="true" android:exported="false" /> <activity android:name=".LockScreenActivity" android:showOnLockScreen="true" android:excludeFromRecents="true" android:theme="@style/LockScreenTheme" android:configChanges="orientation|keyboardHidden|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> ``` 这样,当用户锁定屏幕时,LockScreenReceiver将接收到广播事件,并启动WordService来后台运行应用程序并提供背单词功能。WordService将定时显示随机单词,并在屏幕上显示它。当用户解锁屏幕时,LockScreenActivity将作为锁屏界面并显示单词。这个应用程序可以帮助用户在碎片化时间内背单词,提高学习效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值