如何开发Android服务依赖于其他服务

时间:2012-07-03 02:55:10

标签: android service dependencies

我有两项服务,服务A和服务B.如何开发服务B取决于服务A?当我启动Service B时,我应该知道Service A是否正在运行。如果Service A正在运行,请启动Service B .Else,而不是启动Service B.

1 个答案:

答案 0 :(得分:0)

所以,你的问题是一样的,如何知道一个服务是否正在运行,对吧;)

Android有Activity Manager来完成您的工作。

public boolean checkService(String service) {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (service.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

字符串服务应该是您的类名,例如:com.app.Intent

这是一个伪代码。我没有详细说明,因为许多启动服务的方式取决于你如何使用它。

if checkService("com.app.ServiceA") 
    running ServiceA;
running ServiceB;  // Because I see that ServiceB always still always run, not depend on ServiceA

希望这可以帮助你:)