如何确定Android服务是否在前台运行?

use*_*985 23 service android foreground

我有一个服务,我相信它已经在前台运行,我如何检查我的实现是否正常?

geo*_*eok 26

public static boolean isServiceRunningInForeground(Context context, Class<?> serviceClass) {
      ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
      for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
         if (serviceClass.getName().equals(service.service.getClassName())) {
            if (service.foreground) {
               return true;
            }

         }
      }
      return false;
   }
Run Code Online (Sandbox Code Playgroud)

  • 我发现此答案中的代码比接受答案中的代码更整洁,即使有超过50个服务正在运行,它也能正常工作,这与接受的答案不同. (6认同)
  • 同样被接受但更清晰,更短,更简单.还要检查所有服务,而不仅仅是50. (3认同)
  • getRunningServices 在 Android 9 中已弃用 (3认同)

pio*_*rpo 23

private boolean isServiceRunning(String serviceName){
    boolean serviceRunning = false;
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
    Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
    while (i.hasNext()) {
        ActivityManager.RunningServiceInfo runningServiceInfo = i
                .next();

        if(runningServiceInfo.service.getClassName().equals(serviceName)){
            serviceRunning = true;

            if(runningServiceInfo.foreground)
            {
                //service run in foreground
            }
        }
    }
    return serviceRunning;
}
Run Code Online (Sandbox Code Playgroud)

如果您想知道您的服务是否在前台运行,只需打开其他胖应用程序,然后检查服务是否仍在运行或只是检查标志service.foreground.

  • 谢谢,效果很好!我只需要添加这一行:serviceInForeground = runningServiceInfo.foreground; (5认同)
  • @ user811985仅供参考,有关RunningServiceInfo.foreground的文档说明:"如果服务*要求*作为前台进程运行,则设置为true.".也就是说,它不会告诉你android是否实际上已经接受了前台服务.http://developer.android.com/reference/android/app/ActivityManager.RunningServiceInfo.html#foreground (4认同)
  • 不幸的是,GetRunningServices 现在被标记为已折旧 (2认同)

VRZ*_*Z78 6

从 API 26 开始,getRunningService()已弃用。

现在的一种解决方案是将您的活动绑定到您的服务。然后,您可以从活动中调用服务的方法,以检查它是否正在运行。

1 - 在您的 Service 中,创建一个扩展 Binder 并返回您的 Service 的类

  public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }
Run Code Online (Sandbox Code Playgroud)

2 - 在您的服务中,声明活页夹

private final IBinder binder = new LocalBinder();
Run Code Online (Sandbox Code Playgroud)

3 - 在您的服务中,实施onBind(),这将返回 Binder

  @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
Run Code Online (Sandbox Code Playgroud)

4 - 在您的服务中,创建一个方法来检查它是否正在运行(例如检查变量是否已初始化)

  public boolean isRunning() {
        // If running
            return true;
        // If not running
            return false;
        
    }
Run Code Online (Sandbox Code Playgroud)

5 - 在您的活动中,创建一个保存您的服务的变量

private MyService myService;
Run Code Online (Sandbox Code Playgroud)

6 - 现在,在您的活动中,您可以绑定到您的服务

private void checkIfEnabled() {
    ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            MyService.LocalBinder binder = (MyService.LocalBinder) service;
            myService = binder.getService();
            
            // Calling your service public method
            if(myService.isRunning()) {
                // Your service is enabled
            } else {
                // Your service is disabled
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {

        }
    };

    // Bind to MyService
    Intent intent = new Intent(this, MyService.class);
    bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看官方文档中的绑定服务概述


Mar*_*ark 5

答案的更有效变体:https : //stackoverflow.com/a/36127260/1275265

public static boolean isServiceRunningInForeground(Context context, Class<?> serviceClass) {
   ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
      if (serviceClass.getName().equals(service.service.getClassName())) {
         return service.foreground;
      }
   }
   return false;
}
Run Code Online (Sandbox Code Playgroud)