简体   繁体   English

android拦截最近的应用程序按钮

[英]android intercept recent apps button

I have an application meant for children and I do not want them to be able to click the "Recent Apps" button (the one that looks like two rectangles on top of each other). 我有一个适用于儿童的应用程序,我不希望他们能够单击“最近的应用程序”按钮(看起来像两个矩形的按钮)。 I am taking care of capturing the back button and the home button and I have searched and read a lot about about trying to capture the Recent Apps button, but most say you cannot or the way they do it is very questionable. 我正在关注捕获后退按钮和主页按钮,我已经搜索并阅读了很多关于尝试捕获最近的应用程序按钮,但大多数人说你不能或他们这样做的方式是非常值得怀疑的。

The App "Kids Place" pops up a view that says "Action Not Allowed" and redirects you to its home screen if you press the Recent Apps button, this works even if you are in a different app, so how are they doing this? 应用程序“Kids Place”会弹出一个“不允许操作”的视图,如果您按下“最近使用的应用程序”按钮,则会将您重定向到其主屏幕,即使您在不同的应用程序中,这也有效,那么他们如何做到这一点?

Any suggestions, hints would be appreciated. 任何建议,提示将不胜感激。

Thanks. 谢谢。

After lots of searching and coding the current best solution I found is the following: 经过大量搜索和编码后,我发现当前最佳解决方案如下:

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

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

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

This requires that you use <uses-permission android:name="android.permission.GET_TASKS" /> 这要求您使用<uses-permission android:name="android.permission.GET_TASKS" />

When using this approach when the user presses the recent apps button it will cause your activity will go through the activity lifecycle as follows: onPause -> onWindowFocusChanged -> onResume. 当用户按下最近的应用程序按钮时使用此方法时,将导致您的活动将经历活动生命周期,如下所示:onPause - > onWindowFocusChanged - > onResume。

To the user the behavior appears that pressing the recent apps button has no response. 对于用户,出现按下最近的应用程序按钮没有响应的行为。 NOTE: that I have found that if you press the recent apps button quickly it will display that view for brief time. 注意:我发现如果您快速按下最近的应用程序按钮,它将在短时间内显示该视图。

This is not the best solution, but it is a stab at it. 这不是最好的解决方案,但它是一个很好的解决方案。 If you have a better solution please share. 如果您有更好的解决方案,请分享。

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. 这不是我自己的代码,但这只是隐藏了最近显示的应用列表。

In the accepted answer you're using ClassName only for Android 4.2 - 4.4. 在接受的答案中,您仅使用ClassName for Android 4.2 - 4.4。 It won't work on 5.0 and higher, or Android 4.1. 它不适用于5.0及更高版本或Android 4.1。

Here is the list of ClassNames for main Android versions: 以下是主要Android版本的ClassNames列表:

  • Android 4.1: "com.android.internal.policy.impl.RecentApplicationsDialog" Android 4.1:“com.android.internal.policy.impl.RecentApplicationsDialog”
  • Android 4.2 - 4.4: "com.android.systemui.recent.RecentsActivity" Android 4.2 - 4.4:“com.android.systemui.recent.RecentsActivity”
  • Android 5.0 - 7.1: "com.android.systemui.recents.RecentsActivity" ("s" letter was added) Android 5.0 - 7.1:“com.android.systemui.recents.RecentsActivity”(添加了“s”字母)

The best solution for you will be to utilize Accessibility Service. 最适合您的解决方案是使用辅助功能服务。 Override onAccessibilityEvent() method, filter out ClassNames listed above and do something when you detect this event. 覆盖onAccessibilityEvent()方法,过滤掉上面列出的ClassNames,并在检测到此事件时执行某些操作。 For example simulate pressing the 'Home' button. 例如,模拟按“主页”按钮。 You can do this by making a global action in Accessibility Service. 您可以通过在辅助功能服务中进行全局操作来完成此操作。

thanks esse for solution for higher SDK! 感谢esse for higher SDK的更高解决方案! I missed it. 我错过了。

But in my case I need to duplicate call (or effect is unstable) 但在我的情况下,我需要重复调​​用(或效果不稳定)

        if (SDK>15){
            windowCloseHandler.postDelayed(windowCloserRunnable, 10);
            windowCloseHandler.postDelayed(windowCloserRunnable, 300);
        }

如果您对禁用所有系统按钮感兴趣,可能会选择完全终止系统栏,请参阅Android ICS上隐藏系统栏的简便方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 拦截/禁用最近的应用程序按钮 - Android intercept/disable recent apps button 如何禁用Android Lollipop中的“最近使用的应用程序”按钮 - how to disable the recent apps button in Android Lollipop 尝试在 android 中禁用最近的应用程序按钮 - Trying to disable recent apps button in android 在 android 应用程序中禁用主页按钮和最近的应用程序按钮 - Disable home button and recent apps button in android application 一次关闭所有最近打开/运行的应用程序按钮 - android - Close all recent open/running apps at once button-android 在Android上覆盖和控制“最近使用的应用程序”按钮时,onBackPressed()等效于什么? - What is the equivalent of onBackPressed() for overriding and taking control of the Recent Apps button on Android? 为什么最近的应用程序在按下android中的后退按钮时显示? - Why recent apps are showing on pressing the back button in android? 如何在Android棉花糖中复制“最近的任务/应用程序”按钮? - How do I replicate the Recent Tasks/Apps button in Android Marshmallow? Android:如果用户使用“最近使用的应用程序”按钮关闭应用程序,则控制动作发生 - Android: Control action to happen if user closes app with Recent Apps Button 如何处理android studio中最近的应用程序导航按钮? - How to handle recent apps navigation button in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM