Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何检查IntentService是否已启动_Android_Intentservice - Fatal编程技术网

Android 如何检查IntentService是否已启动

Android 如何检查IntentService是否已启动,android,intentservice,Android,Intentservice,我想知道某个活动是否成功启动了IntentService 由于可以通过bindService()绑定IntentService以保持其运行,因此,一种方法可能是检查调用startService(intent)是否会导致调用服务对象中的onStartCommand(…)或onHandleIntent(…) 但是如何在活动中检查呢?以下是我用来检查服务是否正在运行的方法。服务类是DroidUptimeService private boolean isServiceRunning() { A

我想知道某个活动是否成功启动了
IntentService

由于可以通过
bindService()
绑定
IntentService
以保持其运行,因此,一种方法可能是检查调用
startService(intent)
是否会导致调用服务对象中的
onStartCommand(…)
onHandleIntent(…)


但是如何在活动中检查呢?

以下是我用来检查服务是否正在运行的方法。服务类是DroidUptimeService

private boolean isServiceRunning() {
    ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);

    if (serviceList.size() <= 0) {
        return false;
    }
    for (int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;
        if (serviceName.getClassName().equals(DroidUptimeService.class.getName())) {
            return true;
        }
    }

    return false;
}
private boolean isServiceRunning(){
ActivityManager ActivityManager=(ActivityManager)getSystemService(ACTIVITY_服务);
List serviceList=activityManager.getRunningServices(整数.MAX_值);
if(serviceList.size()
我想知道某个活动是否成功启动了IntentService

如果调用
startService()
时,活动或服务中均未出现异常,则
IntentService
已启动

因为可以通过bindService()绑定IntentService以保持其运行


为什么?

您可以在构造
PendingEvent
时添加一个标志,如果返回值为
null
,则您的服务不会启动。提到的标志是
PendingEvent.flag\u NO\u CREATE

Intent intent = new Intent(yourContext,YourService.class);
PendingIntent pendingIntent =   PendingIntent.getService(yourContext,0,intent,PendingIntent.FLAG_NO_CREATE);

if (pendingIntent == null){
    return "service is not created yet";
} else {
    return "service is already running!";
}

以下是我用来检查我的服务是否正在运行的方法:

  public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return service.started;
            }
        }
        return false;
    }
public静态布尔isMyServiceRunning(类serviceClass,上下文){
ActivityManager=(ActivityManager)context.getSystemService(ACTIVITY_服务);
对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(service.service.getClassName())){
返回服务已启动;
}
}
返回false;
}

谢谢..但这不是我要搜索的-我需要检查startService()是否调用,而不是在服务实际运行时调用…嗯,他们必须广播您的服务,并打算告诉您它已启动。对我来说,这似乎是唯一的选项。我只想确保服务已启动。感谢只有在您使用PendingEvent(例如AlarmManager)启动服务时,此功能才会起作用该标志的doc声明:该标志指示如果所描述的pendingent不存在,则只返回null而不是创建它。对于长时间运行的服务,该标志将不起作用