Android禁用home/recent键的方法

禁用home/recent键步骤如下,成功调用的前提是app要系统签名(platform key)
1. 定义flags变量如下
public static final int DISABLE_NONE =0x00000000;
public static final int DISABLE_HOME =0x00200000;
public static final int DISABLE_RECENT =0x01000000;
【Android禁用home/recent键的方法】public static final int DISABLE_EXPAND =0x00010000;
2. 在onWindowFocusChanged方法中调用disableStatusBar
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
disableStatusBar(DISABLE_HOME |DISABLE_RECENT |DISABLE_EXPAND);
}
3. 在onStop中还原status bar状态,在定要还原,否则系统无法正常使用。
@Override
protected void onStop() {
super.onStop();
Log.e(TAG,"#################onStop");
disableStatusBar(DISABLE_NONE);
}
public void disableStatusBar(int flags) {
try {
Object service = getSystemService("statusbar");
Class statusBarManager = Class.forName("android.app.StatusBarManager");
Method disable = statusBarManager.getMethod("disable",int.class);
disable.invoke(service, flags);
}catch (Exception e) {
Log.e(TAG,"#################disable error:", e);
}
}

    推荐阅读