我正在使用startActivity(intent)
来启动应用程序。在启动应用程序之前,我需要知道应用程序是从后台恢复还是重新启动(即它不在后台)。
在Lollipop之前,我曾经获取最近的任务列表并检查它是否有一个匹配包名的应用程序。
但是在Lollipop中我不能使用getRecentTask
,所以这不会有帮助。
刚试用,我也试过了:
originalIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if(originalIntent != null){
mainActivityIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(originalIntent.equals(mainActivityIntent))
Log.d("temp", "New Task");
else
Log.d("temp", "Background Task");
}
没有任何成功。
可以做些什么?
注意:我转换到的其他应用程序可以是手机上安装的任何其他应用程序,而不是我的应用程序。
答案 0 :(得分:1)
使用onRestart和onResume方法
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
答案 1 :(得分:1)
当重新启动活动时(不在exec中)总是执行onCreate,而不是来自后台。
在onCreate方法中设置一个变量:
isNewLaunch = true;
并在onResume:
中查看if(isNewLaunch) {
} else {
}
在onPause或onStop
中设置falseisNewLaunch = false;