How to disable the Recent Tasks/Apps button

后端 未结 5 741
忘掉有多难
忘掉有多难 2020-11-30 02:48

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

相关标签:
5条回答
  • 2020-11-30 03:04

    To the best of my knowledge, you cannot override the behavior of the home button (like the way you can override the behavior of the BACK button) without the user confirming it. In other words, the defaults of the app that currently handles the home button -- which is whatever the current home launcher is -- would have to be cleared first.

    0 讨论(0)
  • 2020-11-30 03:04

    You can disable StatusBar popups:

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        try {
            if (!hasFocus) {
                Object service = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse.setAccessible(true);
                collapse.invoke(service);
            }
        } catch (Exception e) {
            Log.e(TAG, "onWindowFocusChanged - " + e.getCause());
        }
    }
    

    Also you should add this permission in AndroidManifest.xml

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

    Hope this helps.

    0 讨论(0)
  • 2020-11-30 03:17

    Actually, you cannot disable task button. When you press it, your activity calls onPause() method and you can move your task to the front in this method. See my answer for related question.

    0 讨论(0)
  • 2020-11-30 03:18

    Have you tried this code?

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

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

    0 讨论(0)
  • 2020-11-30 03:31

    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" />
    
    0 讨论(0)
提交回复
热议问题