2025-05-24

 

花瞭半天時間研究下瞭自定義鎖屏,發現其實實現並不是很神秘。不過有些地方還是值得註意。

首先說流程,鎖屏界面一般是在關閉屏幕時啟用,打開屏幕時展現在我們面前,所以我們知道瞭鎖屏的時機,恰好屏幕開關會發出相應的廣播,所以我們也可以像系統一樣捕獲到屏幕開關的事件。

廣播對應的兩個action

 

引用

 

android.intent.action.SCREEN_ON

android.intent.action.SCREEN_OFF

 

 

有趣的是我在我的me860上還看到瞭兩個廣播

 

引用

 

android.intent.action.batteryprofile.SCR_OFF

android.intent.action.batteryprofile.SCR_ON

 

 

這可能是moto自己定義的廣播,看大傢需要瞭。

如果單單隻截獲到廣播,鎖屏的界面怎麼替換系統鎖屏呢?其實我們這裡並沒有替換掉系統鎖屏,隻是我們自己的鎖屏開啟瞭系統鎖屏,從而達到瞭一個替換的效果。

這裡還是要註意點,上述action不能在AndroidManifest.xml中註冊,不能觸發。所以這裡我采用的方法是放在一個service中動態註冊,截獲廣播正常。

這裡還順帶提下service防殺的功能,因為service的進程如果不存在瞭,那麼屏幕開關的廣播是無論如何也接收不到的。

如果是系統回收掉的service,系統會在過段時間,資源充足的情況下再啟動起來,不過我們可不想自己的service坐冷板凳,所以對付系統關閉service我們采取如下方法

在service生命周期中

Java代碼 

Intent startIntent=null;  

@Override 

    public void onStart(Intent intent, int startId) {  

        startIntent=intent;  

        IntentFilter filter=new IntentFilter();  

        filter.addAction(Intent.ACTION_SCREEN_ON);  

        filter.addAction(Intent.ACTION_SCREEN_OFF);  

        receiver=new RelativeBroadcastReceiver();  

        registerReceiver(receiver, filter);  

        System.out.println("service onStart and action is "+intent.getAction());  

        System.out.println("service onStart and startId is "+startId);  

    };  

      

    @Override 

    public void onDestroy() {  

        // TODO Auto-generated method stub  

        System.out.println("service onDestroy");  

        unregisterReceiver(receiver);  

        if(startIntent!=null){  

            System.out.println("serviceIntent not null");  

            startService(startIntent);  

        } 

 

Intent startIntent=null;

@Override

       public void onStart(Intent intent, int startId) {

              startIntent=intent;

              IntentFilter filter=new IntentFilter();

              filter.addAction(Intent.ACTION_SCREEN_ON);

              filter.addAction(Intent.ACTION_SCREEN_OFF);

              receiver=new RelativeBroadcastReceiver();

              registerReceiver(receiver, filter);

              System.out.println("service onStart and action is "+intent.getAction());

              System.out.println("service onStart and startId is "+startId);

       };

      

       @Override

       public void onDestroy() {

              // TODO Auto-generated method stub

              System.out.println("service onDestroy");

              unregisterReceiver(receiver);

              if(startIntent!=null){

                     System.out.println("serviceIntent not null");

                     startService(startIntent);

              }

 

 

我們保留瞭開啟service的intent,當進入ondestroy周期中時再啟動一次自己,系統看到你這勁頭也隻好答應你常駐內存瞭。

 

不過如果是一些內存管理軟件殺掉瞭程序進程,以上方法就沒用瞭,我們還是可以通過截獲一些關鍵廣播來啟動自己的service,類似91助手等就是這樣。可以監聽wifi連接,電池電量發生變化等廣播來啟動自己的service。

 

保證自己的service常駐,這時候就該我們自己的廣播接收器發揮作用瞭。

Java代碼 

@Override 

    public void onReceive(Context context, Intent intent) {  

        // TODO Auto-generated method stub  

        String action=intent.getAction();  

        System.out.println("action is "+action);  

        Intent lockIntent=new Intent(context,MyLockScreen.class);   

        lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

        context.startActivity(lockIntent);  

 

@Override

       public void onReceive(Context context, Intent intent) {

              // TODO Auto-generated method stub

              String action=intent.getAction();

              System.out.println("action is "+action);

              Intent lockIntent=new Intent(context,MyLockScreen.class);

              lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

              context.startActivity(lockIntent);

}

 

需要加上lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);沒有加的時候系統會報錯,但是加上以後也有問題,這會導致多次退出才能退出自定義的鎖屏界面。

其實可以自定義一個stack來管理這些activity,有其他辦法的同學請提示我改正。

 

啟動瞭一個activity以後我們發現還是原來的鎖屏界面,這也是前面提到的,我們的鎖屏需要打開系統鎖屏。

打開系統鎖屏:

Java代碼 

super.onCreate(savedInstanceState);   

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);  

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);  

        setContentView(R.layout.main); 

 

super.onCreate(savedInstanceState);

              getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

              getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

              setContentView(R.layout.main);

 

添加瞭這兩個flag以後,系統鎖屏就被替換成瞭我們自己的鎖屏界面。

鎖屏替換功能也就完成瞭。其實還可以修改系統鎖屏,不過設計到框架層的修改,推廣也很麻煩,所以掠過不說瞭

 

作者wiseideal

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *