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

时间:2011-06-23 10:19:15

标签: android service foreground

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

6 个答案:

答案 0 :(得分:22)

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

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

答案 1 :(得分:21)

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

答案 2 :(得分:3)

更有效的答案变体: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;
}

答案 3 :(得分:1)

从API 26开始,getRunningService()已过时。

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

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

  public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

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

private final IBinder binder = new LocalBinder();

3-在您的服务中,实施onBind(),它将返回活页夹

  @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

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

  public boolean isRunning() {
        // If running
            return true;
        // If not running
            return false;
        
    }

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

private MyService myService;

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

有关更多信息,请查阅官方文档中的Bound services overview

答案 4 :(得分:0)

一点点关于亚当的答案:

@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceForegrounded(service: Class<T>) =
    (getSystemService(ACTIVITY_SERVICE) as? ActivityManager)
        ?.getRunningServices(Integer.MAX_VALUE)
        ?.find { it.service.className == service.name }
        ?.foreground == true

答案 5 :(得分:-1)

这在Coinverse应用程序中处理了加密新闻。

这是最简洁的 Kotlin 解决方案。感谢此Abbas Naqdi中的GitHub issue

@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceRunning(service: Class<T>) =
        (getSystemService(ACTIVITY_SERVICE) as ActivityManager)
            .getRunningServices(Integer.MAX_VALUE)
            .any { it.service.className == service.name }