简体   繁体   中英

How to check whether activity is in foreground or background in android?

I have a activity in which in onCreate method I have to check whether activity is in foreground or background .if activity is in foreground then I have to send request to to server and if app goes in background then I have to send that server request through Intent service.How can I do that.

Is there any way if app goes in background stop server request which is in foreground .

Try This for check your app is in foreground or not

public static boolean isAppInForeground(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx
            .getApplicationContext().getSystemService(
                    Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> services = activityManager
            .getRunningTasks(Integer.MAX_VALUE);

    if (services == null) {
        return false;
    }

    if (services.size() > 0
            && services.get(0).topActivity
                    .getPackageName()
                    .toString()
                    .equalsIgnoreCase(
                            ctx.getApplicationContext().getPackageName()
                                    .toString())) {
        return true;
    }

    return false;
}

Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods.

Example Implement custom Application class :

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}

Register your application class in AndroidManifest.xml

and then so something like this :

@Override
protected void onResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@Override
protected void onPause() {
  super.onPause();
  MyApplication.activityPaused();
}

最好的方法是,您应该在活动的onStop()调用中触发服务,因为该服务启动时,onStatCommand()中的逻辑将被执行,并且如果您希望服务不依赖于应用程序生命周期,则返回带有in的START_STICKY onStatCommand()。

Here is a solution using Application class.

public class AppSingleton extends Application implements Application.ActivityLifecycleCallbacks {

private WeakReference<Context> foregroundActivity;


@Override
public void onActivityResumed(Activity activity) {
    foregroundActivity=new WeakReference<Context>(activity);
}

@Override
public void onActivityPaused(Activity activity) {
    String class_name_activity=activity.getClass().getCanonicalName();
    if (foregroundActivity != null && 
            foregroundActivity.get().getClass().getCanonicalName().equals(class_name_activity)) {
        foregroundActivity = null;
    }
}

//............................

public boolean isOnForeground(@NonNull Context activity_cntxt) {
    return isOnForeground(activity_cntxt.getClass().getCanonicalName());
}

public boolean isOnForeground(@NonNull String activity_canonical_name) {
    if (foregroundActivity != null && foregroundActivity.get() != null) {
        return foregroundActivity.get().getClass().getCanonicalName().equals(activity_canonical_name);
    }
    return false;
}
}

If you have a reference to the required Activity or using the canonical name of the Activity, you can find out whether it's in the foreground or not. This solution may not be foolproof. Therefore your comments are really welcome.

Check this library. It can ctrack current activity status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM