Java 如何监控我的应用程序是否从前台运行切换到后台运行,反之亦然?

Java 如何监控我的应用程序是否从前台运行切换到后台运行,反之亦然?,java,android,android-studio,Java,Android,Android Studio,我想知道我的应用程序在前台运行和后台运行之间切换,反之亦然 我获得了以下代码来检测我的应用程序是否在后台运行: private boolean isAppIsInBackground(Context context) { boolean isInBackground = true; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if

我想知道我的应用程序在前台运行和后台运行之间切换,反之亦然

我获得了以下代码来检测我的应用程序是否在后台运行:

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

现在我需要以某种方式注册上述广播接收器:

IntentFilter appStateFfilter = // ???? this is the part I do not know about
context.registerReceiver(appStateReceiver, appStateFfilter);
我不会使用onPause和onResume,因为我应该在每项活动中都这样做

如何注册BroadcastReceiver以监控应用程序是否切换状态,即前台运行、后台运行?除了我的方法之外,还有其他解决方案可以在应用程序状态更改时通知您吗

您可以编写一个服务类,用于监视您的应用程序是在前台还是后台运行,并可以从那里注册您的广播接收器

private boolean isAppIsInBackground(Context context) {
    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())) {
                         Handler mmHandler = new Handler(getMainLooper());
                        mmHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                //code
                                }
                            });
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
            Handler mmHandler = new Handler(getMainLooper());
                mmHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //code
                        }
                    });
        }
    }

    return isInBackground;
}

你能帮我写一些索赔的代码吗。我不知道如何进行监视。我如何启动服务?启动服务新意图应用程序上下文,Service.class;将Service.class替换为您的类名我在哪里放置Intent?对您的活动使用onPause和onResume方法如何?我不会使用onPause和onResume,因为我应该在每个活动中都使用它们。然后创建一个基本活动,并使您的所有活动都扩展它。
private boolean isAppIsInBackground(Context context) {
    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())) {
                         Handler mmHandler = new Handler(getMainLooper());
                        mmHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                //code
                                }
                            });
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
            Handler mmHandler = new Handler(getMainLooper());
                mmHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //code
                        }
                    });
        }
    }

    return isInBackground;
}
  Intent broadcastIntent = new Intent(); 
                                        broadcastIntent.setAction(com.example.intent.action.MESSAGE_PROCESSED); 
                                        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); 
                                        sendBroadcast(broadcastIntent);