Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the Recent Tasks/Apps button

Tags:

android

I'm building a child play application for Android. I need to disable all keys when it is in use. I have set the Application as the Home App and disabled the back key (that takes care of the Home and Back button). In order to clear the recent tasks list I've created a list of Dummy Activites which start and then finish when the application starts. The Dummy Activites look like:

public class Dummy1 extends Activity
{
  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
   finish();
}
}

And then in my onCreate of the application I perform:

this.pm.setComponentEnabledSetting(new ComponentName(this, Dummy1.class),   PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent localIntent1 = new Intent(this, Dummy1.class);
localIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localIntent1); 

This takes care of when someone tries to hold the Home key to display the recent tasks as I create 8 of these and they're all empty so the user can't click to change apps.

Now the only button I can't seem to disable is the "Recent Tasks/Apps" button (available mainlyon HTC devices, i.e. One X, One S, etc.). This button seems to still bring up all the recent tasks (even though my dummy tasks were created) and I can't seem to find a "hook" for the event that is fired when this button is pressed?

Note: I know it's do-able since apps like ToddlerLock have done it....I just can't seem to figure it out.

like image 941
Mace Avatar asked Jan 29 '13 01:01

Mace


1 Answers

This will close the RecentActivity dialog. Put it in your activity class.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (!hasFocus) {
            windowCloseHandler.postDelayed(windowCloserRunnable, 250);
        }
    }

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

You will need to put the following permission in your manifest.

    <uses-permission android:name="android.permission.GET_TASKS" />
like image 154
mWhitley Avatar answered Oct 14 '22 11:10

mWhitley