0

I have a launcher with two profile: parent and child the child can not access to the store and settings. I am using a tablet 8". home button and access to recent tasks button are not in the status bar. My problem that if child access to recent tasks. He will be able to access to the settings. So how can I disable access to recent tasks. I tried the code given in disable access to recent tasks, but it didn't work for me. I tried:

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

It hid the access, but it is not an effective solution. The child may access to the status bar so to settings.

Howli
  • 12,291
  • 19
  • 47
  • 72
user2641131
  • 1
  • 1
  • 4
  • possible duplicate http://stackoverflow.com/a/17774643/2520370 – Steve Sep 16 '13 at 14:54
  • i said that i used such approach but it is not what i am searching for because we can acces to recent tasks with this solution – user2641131 Sep 16 '13 at 14:57
  • 1
    If you want to disable the recent dialog you'll need an exploit because it's intentionally not possible. – zapl Sep 16 '13 at 15:12
  • Possible duplicate of [How to disable the Recent Tasks/Apps button in Android](http://stackoverflow.com/questions/14574239/how-to-disable-the-recent-tasks-apps-button-in-android) – blahdiblah May 20 '14 at 22:49

2 Answers2

3

The best way I have found is to do this:

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/

This is not my own code, but this just hides the recent apps list from showing.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
0

The best way I found to use this method for close 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);
}

So, when and how to use this method ?

Override onPause() in your activity and first check for recent activity in ActivityManager. this method is NOT not accurate, but I didn't find a better way:

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

I checked in on android 4.4.2 and it works, hope this will help you too.

Also you need to put permission in AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS"/>

If you need also to close StatusBar you can try this code:

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;

}

this methods also requires 2 permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>