有没有办法处理活动的任务切换器?

Is there a way to handle the task switcher active?

所以我们的应用程序正在通过在其他地方发现的这个 hack 进入后台时进行处理:

 public static boolean isAppGoingToBackground(Context context)
    {
        boolean retVal = false;
        ActivityManager mgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTasks = mgr.getRunningTasks(1);
        if (!runningTasks.isEmpty())
        {
            // If the package name of the current activity doesn't equal the context's
            // package name, then the user is no longer in the context's app.
            ComponentName currentActivity = runningTasks.get(0).topActivity;
            retVal = !currentActivity.getPackageName().equals(context.getPackageName());
        }
        return retVal;
    }

但是,如果通过电源按钮关闭屏幕,这将无法处理,因此我们为该事件添加了一个广播接收器:

_receiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                if(intent.getAction() == Intent.ACTION_SCREEN_ON)
                {
                    //resume
                }
                else if(intent.getAction() == Intent.ACTION_SCREEN_OFF)
                {
                    handleAppGoingToBackground(_context);
                }
            }
        };

然而,当用户使用任务切换器将应用置于后台时,这些都没有给出正确的响应:

如果您以这种方式为应用设置后台,则 isGoingToBackground returns 为 false。我希望广播接收器中有另一个事件可以用来处理这个事件,但我还没有找到任何东西。无论如何,当您的应用以这种方式作为背景时,是否需要知道它?

我建议使用 onPause() 方法。因此,如果您的应用转到 'bagrounding',则会调用 onPause() 方法。就像 onCreate() 一样,只要您的应用程序进入后台,您的应用程序就会运行 onPause() 方法。您还可以使用 BroadcastReceiver 检查您的应用程序是如何通过屏幕关闭或用户发送到后台的。如果您想了解有关如何执行此操作的更多信息。在下方评论。

好的,所以我得到了一个可行的解决方案,我采用了一些不同的方法来使用 getTasks 调用,因为它已被弃用。到目前为止,这似乎适用于我测试过的所有内容,但我会继续测试,看看是否还有其他问题。基本上我所有的活动都有一个超级 class 定义如下:

public abstract class BaseActivity extends AppCompatActivity
{
    boolean isStartingActivity = false;
    boolean appWentToBackground = false;
    @Override
    protected void onPostResume()
    {
        super.onPostResume();

        if (appWentToBackground) 
        {
            appWentToBackground = false;
            handleAppComingToForeground();
        }
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        if (!isFinishing() && !isStartingActivity && getChangingConfigurations() == 0)
        {
            appWentToBackground = true;
            //If all of the above is true the app must be going to background
            handleAppGoingToBackground();
        }
        isStartingActivity = false;
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options)
    {
        isStartingActivity = true;
        super.startActivityForResult(intent, requestCode, options);
    }

    @Override
    public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
    {
        isStartingActivity = true;
        super.startActivityFromFragment(fragment, intent, requestCode);
    }
}

思考过程是 ChangingConfigs 检查处理旋转,isFinishing 处理后退(它不处理返回到另一个应用程序中的点,但在我的情况下这种情况没问题),isStartingActivity 处理这种情况我正在过渡到另一个 activity,如果满足所有这些条件,那一定意味着我正在以某种形式或其他形式进行背景设置(适用于我的应用程序切换器和正常背景)。

如果我注意到任何其他可以改进的地方,或者我错过的案例,我会更新。