我有一个带有两个配置文件的启动器:父和子,孩子无法访问商店和设置。我正在使用平板电脑8“。主页按钮和访问最近任务按钮不在状态栏。我的问题是,如果孩子访问最近的任务。他将能够访问设置。所以我怎么能禁用访问最近的任务。我尝试了disable access to recent tasks中给出的代码,但它对我没用。我试过了:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus) {
windowCloseHandler.postDelayed(windowCloserRunnable, 10);
}
}
private void toggleRecents() {
Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
ComponentName recents = new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
closeRecents.setComponent(recents);
this.startActivity(closeRecents);
}
private Handler windowCloseHandler = new Handler();
private Runnable windowCloserRunnable = new Runnable() {
@Override
public void run() {
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
toggleRecents();
}
}
};
它隐藏了访问权限,但它不是一个有效的解决方案。孩子可以访问状态栏以进行设置。
答案 0 :(得分:3)
我找到的最好的方法就是这样做:
public class BaseActivity extends Activity {
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
}// all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
这不是我自己的代码,但这只是隐藏了最近显示的应用列表。
答案 1 :(得分:1)
我发现使用此方法关闭RecentsActivity的最佳方法
private static final String RECENTS_ACTIVITY = "com.android.systemui.recent.RecentsActivity";
private static final String SYSTEM_UI_PACKAGE_NAME = "com.android.systemui";
private void closeRecents() {
sendBroadcast(new Intent("com.android.systemui.recent.action.CLOSE"));
Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
ComponentName recents = new ComponentName(SYSTEM_UI_PACKAGE_NAME, RECENTS_ACTIVITY);
closeRecents.setComponent(recents);
startActivity(closeRecents);
}
那么,何时以及如何使用这种方法?
覆盖活动中的onPause()并首先检查ActivityManager中的最近活动。这种方法并不准确,但我没有找到更好的方法:
private ComponentName getTopActivity(){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo runningTaskInfo = taskInfo.get(0);
ComponentName topActivity = runningTaskInfo.topActivity;
return topActivity;
}
@Override
protected void onPause() {
super.onPause();
if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)){
closeRecents();
}else {
validateAgainWithDelay(300);
}
}
private void validateAgainWithDelay(long delay){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (getTopActivity().getClassName().equals(RECENTS_ACTIVITY)) {
closeRecents();
}
}
},delay);
}
我检查了android 4.4.2并且它有效,希望这对你也有帮助。
您还需要在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.GET_TASKS"/>
如果您还需要关闭StatusBar,可以尝试以下代码:
private final Handler uiHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what==REMOVE_STATUS_BAR){
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
collapseStatusBar();
return true;
}
return false;
}
});
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus){
uiHandler.sendEmptyMessage(REMOVE_STATUS_BAR);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 500);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 1000);
uiHandler.sendEmptyMessageDelayed(REMOVE_STATUS_BAR, 2000);
}
}
private void collapseStatusBar() {
try {
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapse = statusbarManager.getMethod(hasCollapseMethod(statusbarManager) ? "collapse" : "collapsePanels");
collapse.setAccessible(true);
collapse.invoke(service);
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean hasCollapseMethod(Class<?> statusbarManager) {
for (Method method : statusbarManager.getMethods()) {
if (method.getName().equals("collapse")) {
return true;
}
}
return false;
}
此方法在AndroidManifest.xml中还需要2个权限
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>