如何禁用"近期任务/应用程序"按钮

Mac*_*ace 14 android

我正在为Android构建一个儿童游戏应用程序.我需要在使用时禁用所有密钥.我已将应用程序设置为主应用程序并禁用后退键(负责主页和后退按钮).为了清除最近的任务列表,我创建了一个Dummy Activites列表,它在应用程序启动时启动然后完成.Dummy Activites看起来像:

public class Dummy1 extends Activity
{
  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
   finish();
}
}
Run Code Online (Sandbox Code Playgroud)

然后在我执行的应用程序的onCreate中:

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); 
Run Code Online (Sandbox Code Playgroud)

当有人试图按住Home键显示最近的任务时,这会照顾我创建其中的8个并且它们都是空的,因此用户无法单击以更改应用程序.

现在,我似乎无法禁用的唯一按钮是"Recent Tasks/Apps"按钮(主要用于HTC设备,即One X,One S等).这个按钮似乎仍然显示所有最近的任务(即使我的虚拟任务已创建),我似乎找不到按下此按钮时触发的事件的"挂钩"?

注意:我知道它是可行的,因为像ToddlerLock这样的应用程序已经完成了......我似乎无法弄明白.

小智 23

这将关闭RecentActivity对话框.把它放在你的活动课上.

    @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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

您需要在清单中添加以下权限.

    <uses-permission android:name="android.permission.GET_TASKS" />
Run Code Online (Sandbox Code Playgroud)


hal*_*aya 7

你试过这段代码吗?

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);
}
}
Run Code Online (Sandbox Code Playgroud)

来自:http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/

  • 这会杀死所有系统对话框.这意味着我甚至无法关闭/或重启我的设备. (3认同)

Lux*_*ode 1

据我所知,如果没有用户确认,您无法覆盖主页按钮的行为(就像覆盖“后退”按钮的行为一样)。换句话说,当前处理主页按钮的应用程序的默认设置(无论当前的主页启动器是什么)都必须首先清除。