Java Android应用程序自动进入前台
在我的应用程序中,在asyntask结束时调用FirstActivity Asynctask,将打开secondactivity 当asyntask运行时应用程序处于后台时 异步任务完成后,应用程序自动进入前台,无需任何用户交互Java Android应用程序自动进入前台,java,android,android-activity,start-activity,Java,Android,Android Activity,Start Activity,在我的应用程序中,在asyntask结束时调用FirstActivity Asynctask,将打开secondactivity 当asyntask运行时应用程序处于后台时 异步任务完成后,应用程序自动进入前台,无需任何用户交互 如何避免它?在启动第二个活动之前,请使用以下方法检查您的应用程序是否处于前台: public static boolean isApplicationSentToBackground(final Context context) { ActivityManage
如何避免它?在启动第二个活动之前,请使用以下方法检查您的应用程序是否处于前台:
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Activity.ACTIVITY_SERVICE);
String className = activityManager.getRunningTasks(1).get(0).topActivity
.getClassName();
if (className.equals("your activity class name with package")) {
//ToDO
}
公共静态布尔值isApplicationSentToBackground(最终上下文){
ActivityManager am=(ActivityManager)context.getSystemService(context.ACTIVITY_服务);
列表任务=am.getRunningTasks(1);
如果(!tasks.isEmpty()){
ComponentName topActivity=tasks.get(0).topActivity;
如果(!topActivity.getPackageName().equals(context.getPackageName())){
返回true;
}
}
返回false;
}
检查是否打开了任何活动
/***
* Checking Whether any Activity of Application is running or not
* @param context
* @return
*/
public boolean isForeground(Context context) {
// Get the Activity Manager
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// Get a list of running tasks, we are only interested in the last one,
// the top most so we give a 1 as parameter so we only get the topmost.
List< ActivityManager.RunningTaskInfo > task = manager.getRunningTasks(1);
// Get the info we need for comparison.
ComponentName componentInfo = task.get(0).topActivity;
// Check if it matches our package name.
if(componentInfo.getPackageName().equals(context.getPackageName()))
return true;
// If not then our app is not on the foreground.
return false;
}
在android中,我们可以发现应用程序位于前台或后台,如下所示:
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Activity.ACTIVITY_SERVICE);
String className = activityManager.getRunningTasks(1).get(0).topActivity
.getClassName();
if (className.equals("your activity class name with package")) {
//ToDO
}