1. 程式人生 > >Android系統應用開發(九)遮蔽狀態列下拉

Android系統應用開發(九)遮蔽狀態列下拉

網上關於遮蔽狀態列的文章搜到不少,但都是針對某個應用,或者鎖屏狀態,才能遮蔽狀態列的下拉,而我的需求是不管任意狀態都遮蔽狀態列下拉,百度到的也可能版本不一樣,說的一些檔案都找不到,搜到一篇文章,自己修改了一個方法,然後OK了,具體如下:

檔案位置:frameworks\base\core\java\android\app\StatusBarManager.java 

修改此方法中的引數,不用傳穿進去的引數

  1. publicvoid disable(int what) {  
  2.        try {  
  3.            mService.disable(DISABLE_EXPAND, mToken, mContext.getPackageName());  
  4.        } catch (RemoteException ex) {  
  5.            // system process is dead anyway.
  6.            thrownew RuntimeException(ex);  
  7.        }  
  8.    }  


參考文章:http://www.xuebuyuan.com/1608099.html

1.做鎖屏軟體,鎖屏軟體具體介面的實現不說,在遮蔽通知欄下拉的時候就出現問題了。網上找了一些資料,可以通過statusbarmanager這個類來實現,由於這個類是系統隱藏的,所以我們很容易就想到使用反射,這個類的原始碼如下:

  1. package android.app;  
  2. import android.content.Context;  
  3. import android.os.Binder;  
  4. import android.os.RemoteException;  
  5. import android.os.IBinder;  
  6. import android.os.ServiceManager;  
  7. publicclass StatusBarManager {  
  8.     publicstaticfinalint DISABLE_EXPAND = 0x00000001;  
  9.     publicstaticfinalint DISABLE_NOTIFICATION_ICONS = 
    0x00000002;  
  10.     publicstaticfinalint DISABLE_NOTIFICATION_ALERTS = 0x00000004;  
  11.     publicstaticfinalint DISABLE_NOTIFICATION_TICKER = 0x00000008;  
  12.     publicstaticfinalint DISABLE_NONE = 0x00000000;  
  13.     private Context mContext;  
  14.     private IStatusBar mService;  
  15.     private IBinder mToken = new Binder();  
  16.     StatusBarManager(Context context) {  
  17.         mContext = context;  
  18.         mService = IStatusBar.Stub.asInterface(  
  19.                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));  
  20.     }  
  21.     publicvoid disable(int what) {  
  22.         try {  
  23.             mService.disable(what, mToken, mContext.getPackageName());  
  24.         } catch (RemoteException ex) {  
  25.             // system process is dead anyway.
  26.             thrownew RuntimeException(ex);  
  27.         }  
  28.     }  
  29.     publicvoid expand() {  
  30.         try {  
  31.             mService.activate();  
  32.         } catch (RemoteException ex) {  
  33.             // system process is dead anyway.
  34.             thrownew RuntimeException(ex);  
  35.         }  
  36.     }  
  37.     publicvoid collapse() {  
  38.         try {  
  39.             mService.deactivate();  
  40.         } catch (RemoteException ex) {  
  41.             // system process is dead anyway.
  42.             thrownew RuntimeException(ex);  
  43.         }  
  44.     }  
  45.     publicvoid toggle() {  
  46.         try {  
  47.             mService.toggle();  
  48.         } catch (RemoteException ex) {  
  49.             // system process is dead anyway.
  50.             thrownew RuntimeException(ex);  
  51.         }  
  52.     }  
  53.     public IBinder addIcon(String slot, int iconId, int iconLevel) {  
  54.         try {  
  55.             return mService.addIcon(slot, mContext.getPackageName(), iconId, iconLevel);  
  56.         } catch (RemoteException ex) {  
  57.             // system process is dead anyway.
  58.             thrownew RuntimeException(ex);  
  59.         }  
  60.     }  
  61.     publicvoid updateIcon(IBinder key, String slot, int iconId, int iconLevel) {  
  62.         try {  
  63.             mService.updateIcon(key, slot, mContext.getPackageName(), iconId, iconLevel);  
  64.         } catch (RemoteException ex) {  
  65.             // system process is dead anyway.
  66.             thrownew RuntimeException(ex);  
  67.         }  
  68.     }  
  69.     publicvoid removeIcon(IBinder key) {  
  70.         try {  
  71.             mService.removeIcon(key);  
  72.         } catch (RemoteException ex) {  
  73.             // system process is dead anyway.
  74.             thrownew RuntimeException(ex);  
  75.         }  
  76.     }  
  77. }  
  78. lt;/span>  

2.如果是系統級應用,也就是手機廠家植入的應用,可以使用disable(int)的方法來進行遮蔽,引數如上原始碼五個引數之一即可。但是如果是在應用層上的,disable方法因為許可權問題無法使用(如果一定要使用必須具有系統簽名)。這個時候可以使用collapse()方法,現在的小米鎖屏和360鎖屏都是使用該方法,具體程式碼如下:

  1. <span style="margin: 0px; padding: 0px; border: 0px; font-size: 12px; background: transparent;">@Override
  2.     publicvoid onWindowFocusChanged(boolean hasFocus) {  
  3.         disableStatusBar();  
  4.         super.onWindowFocusChanged(hasFocus);  
  5.     }  
  6.     publicvoid disableStatusBar(){  
  7.         try {  
  8.             Object service = getSystemService("statusbar");  
  9.             Class<?> claz = Class.forName("android.app.StatusBarManager");  
  10.             Method expand = claz.getMethod("collapse");  
  11.             expand.invoke(service);  
  12.         } catch (Exception e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }</span>  

3.

重寫activity的onWindowfocuschanged方法,執行如上操作即可。以上方法使用了反射,如果不想使用使用反射獲得隱藏的StatusBarManager,我這裡提供一個jar包,將jar包匯入到專案中,即可直接使用StatusBarManager ,還可以直接使用ServiceManager這個隱藏類,它有什麼用?相信做過自動結束通話電話的童鞋應該瞭解。