简体   繁体   English

如何检查活动是在Android中的前景还是背景?

[英]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. 我有一个活动,其中必须在onCreate方法中检查活动是在前台还是在后台。如果活动在前台,则必须将请求发送到服务器,如果应用程序在后台,则必须通过发送该服务器请求专心服务。我该怎么做。

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. 您自己使用Activity.onPause,Activity.onResume方法跟踪应用程序的可见性。

Example Implement custom Application class : 示例实现自定义Application类:

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 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. 这是使用Application类的解决方案。

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. 它可以跟踪当前活动状态。

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

相关问题 如何检查活动是否为前台,并在活动为前台时将其带入后台? - How to check whether a activity is foreground and bring it to background when it is foreground? 如何检查应用程序是在前台还是后台 - How to check whether the app is in foreground or background 如何检查活动是在前景中还是在可见背景中? - How to check if activity is in foreground or in visible background? 如何检查应用程序是在后台还是前台android。 使用以下代码但无法检查 - How to check whether a app is in background or foreground android. Using following code but unable check 如何将前台的Android活动置于后台 - How to put in foreground an Android activity that is in background 如何检查前台 Android 应用活动 - How to check foreground Android app activity 在Android中从后台恢复活动后如何检查用户是否在同一视图上 - How to check if user is on same view after resumes the activity from background to foreground in android 如何检查Android应用程序是在后台运行还是在前台运行? - How to check if an Android application is running in the background or in foreground? 如何查找应用程序是在后台运行还是在前台运行或被杀死的android - How to find whether app is running in background or foreground or killed android 如何从服务检查活动是否在后台/前台运行? - How to check if an activity is running in background/foreground from a service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM