How to detect “Recent Apps” system button clicks (Honeycomb+)

前端 未结 8 715
一个人的身影
一个人的身影 2020-11-30 05:37

I\'m wondering what method this button calls.

\"enter

My game always pauses/re

相关标签:
8条回答
  • 2020-11-30 06:30

    The best way I have found is listent to Broadcast Action called "ACTION_CLOSE_SYSTEM_DIALOGS".From the Google docs:

    Broadcast Action: This is broadcast when a user action should request a temporary system dialog to dismiss. Some examples of temporary system dialogs are the notification window-shade and the recent tasks dialog.

    Working code:

    IntentFilter intentFilterACSD = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                        //do what you want here
                }
            }
        };
        this.registerReceiver(broadcastReceiver, intentFilterACSD);
    
    0 讨论(0)
  • 2020-11-30 06:33

    I wrote an app that unambiguously demonstrates the behavior of the recent-apps / recents key with respect to both Activities and Fragments.

    (Any assertion that normal lifecycle events are not triggered by this key is patently false.)

    In onCreate() the main Activity initializes an array for the logging of all known Activity lifecycle events and also fires the main Fragment alongside itself in a second FrameLayout.

    In onAttach() the main Fragment initializes a separate array for the logging of all known Fragment lifecycle events.

    For logging purposes, both Activity and Fragment treat onWindowFocusChanged() as just another lifecycle event.

    Logged events are timestamped so a user 'pause' indication can be given when rendering (and rendering occurs inside onWindowFocusChanged() when hasFocus==true).

    Here is what user sees when the test app is started:

    After a few seconds user presses recent-apps, waits a few seconds, then presses it again to see the truth:

    NB: If, instead of using the recent-apps button, the experiment is repeated by (a) pressing tablet off/on button (b) pressing Start 2nd Activity button(c) etc., identical results are yielded.

    Answer to OP:
    Apparently there is no code that can detect the actual pressing of the recent-apps key!

    But it doesn't matter because the app needs to be correctly handling the associated lifecycle events anyway.

    0 讨论(0)
提交回复
热议问题