0

我有一个带有两个配置文件的启动器:父母和孩子,孩子无法访问商店和设置。我正在使用平板电脑 8"。主页按钮和访问最近任务按钮不在状态栏中。我的问题是,如果孩子访问最近任务。他将能够访问设置。那么我该如何禁用访问最近的任务。我尝试了禁用访问最近任务中给出的代码,但它对我不起作用。我试过:

@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();
        }
    }
};

它隐藏了访问权限,但这不是一个有效的解决方案。孩子可以访问状态栏以便进行设置。

4

2 回答 2

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/

这不是我自己的代码,但这只是隐藏了最近的应用程序列表。

于 2013-12-06T09:56:54.387 回答
0

我发现使用此方法关闭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"/>
于 2015-02-10T10:27:38.343 回答