Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

android - How to disable the Recent Tasks/Apps button

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...