如何检查应用程序是在后台还是前台android中。正在使用以下代码,但无法检查

如何检查应用程序是在后台还是前台android中。正在使用以下代码,但无法检查,android,push-notification,background-foreground,Android,Push Notification,Background Foreground,我正在尝试使用以下代码查找“推送通知”的前台或后台,但它同时执行后台和前台。有什么解决办法吗 public static boolean isAppIsInBackground(Context context) { boolean isInBackground = true; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (Buil

我正在尝试使用以下代码查找“推送通知”的前台或后台,但它同时执行后台和前台。有什么解决办法吗

public static boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }
    return isInBackground;
}
public静态布尔值isAppIsInBackground(上下文){
布尔值isInBackground=true;
ActivityManager am=(ActivityManager)context.getSystemService(context.ACTIVITY_服务);
if(Build.VERSION.SDK\u INT>Build.VERSION\u code.KITKAT\u WATCH){
List runningprocesss=am.getrunningappprocesss();
对于(ActivityManager.RunningAppProcessInfo processInfo:RunningProcesss){
if(processInfo.importance==ActivityManager.RunningAppProcessInfo.importance\u前台){
for(字符串activeProcess:processInfo.pkgList){
if(activeProcess.equals(context.getPackageName())){
isInBackground=假;
}
}
}
}
}否则{
List taskInfo=am.getRunningTasks(1);
ComponentName componentInfo=taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(context.getPackageName())){
isInBackground=假;
}
}
返回背景;
}

因此,如果我理解您的问题,您想检查当前线程是否是主线程/某个后台线程,您可以这样检查:

if(Looper.myLooper() == Looper.getMainLooper()){
  //you are on your main Thread (also called UI Thread)
}

因此,如果我理解你的问题,你想检查你的当前线程是否是主线程/一些后台线程,你可以这样检查:

if(Looper.myLooper() == Looper.getMainLooper()){
  //you are on your main Thread (also called UI Thread)
}

让您的应用程序类实现LifecycleObserver接口,您可以定义如下所示的方法来获取应用程序前台和应用程序后台事件的回调

class MyApplication implements LifecycleObsercer {

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {
        Timber.d("App in background");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        Timber.d("App is in foreground");
    }
}

让您的应用程序类实现LifecycleObserver接口,您可以定义如下所示的方法来获取应用程序前台和应用程序后台事件的回调

class MyApplication implements LifecycleObsercer {

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {
        Timber.d("App in background");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        Timber.d("App is in foreground");
    }
}

我使用了本教程:使用ProcessLifecycleOwner是否更好?它由安卓系统提供,工作正常。我可以分享一个简单的例子。可能重复我使用过的这个教程:使用ProcessLifecycleOwner会更好吗?它由安卓系统提供,工作正常。我可以分享一个简单的例子。可能是重复的