Android 是否有办法处理激活的任务切换程序?
因此,我们的应用程序正在处理该应用程序何时通过其他地方发现的黑客进入后台:Android 是否有办法处理激活的任务切换程序?,android,broadcastreceiver,task-switching,Android,Broadcastreceiver,Task Switching,因此,我们的应用程序正在处理该应用程序何时通过其他地方发现的黑客进入后台: public static boolean isAppGoingToBackground(Context context) { boolean retVal = false; ActivityManager mgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); Lis
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;
}
但是,当用户使用任务切换程序对应用程序进行后台操作时,这两个选项都不会给出正确的响应:
如果以这种方式对应用程序进行背景设置,则isGoingToBackground返回false。我希望广播接收器中有另一个事件,我可以用来处理这个事件,但我没有找到任何东西。你的应用程序中是否有这样的背景信息?我建议使用
onPause()
方法。因此,如果您的应用程序转到“bagrounding”,将调用onPause()
方法。这就像onCreate()
一样,只要你的应用程序进入后台,你的应用程序就会简单地运行方法onPause()
。您还可以使用BroadcastReceiver
检查您的应用程序是如何通过屏幕断电或用户发送到后台的。如果你想知道更多关于如何做的信息。下面的评论。好的,所以我得到了一个有效的解决方案,我采用了一种稍微不同的方法来使用gettask调用,因为它已被弃用。到目前为止,这似乎对我测试过的所有东西都有效,但我会继续测试,看看是否还有其他问题。基本上,我的所有活动都有一个这样定义的超类:
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);
}
}
思维过程是改变配置检查手柄旋转,完成手柄回压(它不能处理返回到另一个应用程序的点,但这种情况在我的情况下是正常的),isStartingActivity处理我转换到另一个活动的情况,如果所有这些条件都得到满足,那一定意味着我是以某种形式的背景(适用于我的应用程序切换程序和普通背景)
如果我注意到任何其他需要改进的地方,或我错过的案例,我会更新。onPause的问题在于它不仅仅需要背景知识。我不需要在活动之间轮换或转换时调用它,我特别需要在应用程序转到后台时调用它。
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);
}
}