Android系統應用開發(九)遮蔽狀態列下拉
阿新 • • 發佈:2019-01-24
網上關於遮蔽狀態列的文章搜到不少,但都是針對某個應用,或者鎖屏狀態,才能遮蔽狀態列的下拉,而我的需求是不管任意狀態都遮蔽狀態列下拉,百度到的也可能版本不一樣,說的一些檔案都找不到,搜到一篇文章,自己修改了一個方法,然後OK了,具體如下:
檔案位置:frameworks\base\core\java\android\app\StatusBarManager.java
修改此方法中的引數,不用傳穿進去的引數
- publicvoid disable(int what) {
- try {
-
mService.disable(DISABLE_EXPAND, mToken, mContext.getPackageName());
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
參考文章:http://www.xuebuyuan.com/1608099.html
1.做鎖屏軟體,鎖屏軟體具體介面的實現不說,在遮蔽通知欄下拉的時候就出現問題了。網上找了一些資料,可以通過statusbarmanager這個類來實現,由於這個類是系統隱藏的,所以我們很容易就想到使用反射,這個類的原始碼如下:
- package android.app;
- import android.content.Context;
- import android.os.Binder;
- import android.os.RemoteException;
- import android.os.IBinder;
- import android.os.ServiceManager;
- publicclass StatusBarManager {
- publicstaticfinalint DISABLE_EXPAND = 0x00000001;
-
publicstaticfinalint DISABLE_NOTIFICATION_ICONS =
- publicstaticfinalint DISABLE_NOTIFICATION_ALERTS = 0x00000004;
- publicstaticfinalint DISABLE_NOTIFICATION_TICKER = 0x00000008;
- publicstaticfinalint DISABLE_NONE = 0x00000000;
- private Context mContext;
- private IStatusBar mService;
- private IBinder mToken = new Binder();
- StatusBarManager(Context context) {
- mContext = context;
- mService = IStatusBar.Stub.asInterface(
- ServiceManager.getService(Context.STATUS_BAR_SERVICE));
- }
- publicvoid disable(int what) {
- try {
- mService.disable(what, mToken, mContext.getPackageName());
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- publicvoid expand() {
- try {
- mService.activate();
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- publicvoid collapse() {
- try {
- mService.deactivate();
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- publicvoid toggle() {
- try {
- mService.toggle();
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- public IBinder addIcon(String slot, int iconId, int iconLevel) {
- try {
- return mService.addIcon(slot, mContext.getPackageName(), iconId, iconLevel);
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- publicvoid updateIcon(IBinder key, String slot, int iconId, int iconLevel) {
- try {
- mService.updateIcon(key, slot, mContext.getPackageName(), iconId, iconLevel);
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- publicvoid removeIcon(IBinder key) {
- try {
- mService.removeIcon(key);
- } catch (RemoteException ex) {
- // system process is dead anyway.
- thrownew RuntimeException(ex);
- }
- }
- }
- lt;/span>
2.如果是系統級應用,也就是手機廠家植入的應用,可以使用disable(int)的方法來進行遮蔽,引數如上原始碼五個引數之一即可。但是如果是在應用層上的,disable方法因為許可權問題無法使用(如果一定要使用必須具有系統簽名)。這個時候可以使用collapse()方法,現在的小米鎖屏和360鎖屏都是使用該方法,具體程式碼如下:
- <span style="margin: 0px; padding: 0px; border: 0px; font-size: 12px; background: transparent;">@Override
- publicvoid onWindowFocusChanged(boolean hasFocus) {
- disableStatusBar();
- super.onWindowFocusChanged(hasFocus);
- }
- publicvoid disableStatusBar(){
- try {
- Object service = getSystemService("statusbar");
- Class<?> claz = Class.forName("android.app.StatusBarManager");
- Method expand = claz.getMethod("collapse");
- expand.invoke(service);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }</span>
3.
重寫activity的onWindowfocuschanged方法,執行如上操作即可。以上方法使用了反射,如果不想使用使用反射獲得隱藏的StatusBarManager,我這裡提供一個jar包,將jar包匯入到專案中,即可直接使用StatusBarManager ,還可以直接使用ServiceManager這個隱藏類,它有什麼用?相信做過自動結束通話電話的童鞋應該瞭解。