简体   繁体   English

Android应用程序自动进入前台

[英]Android app automatically coming to foreground

In my application in FirstActivity Asynctask is called at the end of asyntask secondactivity will be opened 在我的FirstActivity应用程序中,异步任务结束时调用了Asynctask,secondactivity将被打开

when app is in background when asyntask is running after completion of async task Application automatically coming to foreground without any user interaction 当应用程序在后台运行时,异步任务完成后运行asyntask时,应用程序自动进入前台,而无需任何用户交互

how to avoid it? 如何避免呢?

In android we can find the app is in foreground or background like this: 在android中,我们可以找到该应用位于前景或背景,如下所示:

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
}

Check if your application is in foreground before starting the second activity using a method like this: 在开始第二个活动之前,请使用以下方法检查应用程序是否处于前台:

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;
}

Check Whether any Activity is opened or Not. 检查是否打开了任何活动。

    /***
     * 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;
    }

Use this like: 像这样使用:

if(isForeground(context)) {
    //Nothing to do
} else {

    /**
          You can do here what you want to do
      **/

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android应用程序的前景用法 - Foreground usage for an Android app Android-检测应用移至背景和前景 - Android - Detect app moving to background and foreground Android,前台服务能否使应用程序继续运行? - Android, does foreground service keep app alive? 仅在应用程序处于前台时才收到Android通知 - Android notifications only received if app is in foreground Android-服务应用程序如何将KeyEvent调度到另一个前景应用程序 - Android - How can a Service App dispatch KeyEvents to another Foreground App android检查请求是否来自我的应用程序 - android check if request is coming from my app Android Java:当应用程序不在前台时如何完全删除ScheduledExecutorService实例 - Android Java : How to completely remove instance of ScheduledExecutorService when App is not in foreground 状态栏通知单击事件Android的后台前景应用程序状态 - Status bar notification click event Android for background foreground app state Android:应用程式离开前景时执行小动作 - Android: Perform Small Action When App Leaves Foreground 杀死并重新启动应用程序后前台服务未停止 - Android Studio - Foreground service not stopping after killing and relaunching app - Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM