4

アクティビティが正常に開始されたかどうかを知りたいのですがIntentService

IntentServiceビアをバインドしbindService()て実行を継続することは可能であるため、おそらく、呼び出しによってサービスオブジェクトstartService(intent)の呼び出しonStartCommand(..)またはonHandleIntent(..)サービスオブジェクトの呼び出しが発生するかどうかを確認することをお勧めします。

しかし、どうすればアクティビティでそれを確認できますか?

4

4 に答える 4

7

サービスが実行されているかどうかを確認するために使用する方法は次のとおりです。Serciveクラスは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;
}
于 2011-08-15T00:59:12.683 に答える
5

を作成するときにフラグを追加できます。PendingIntent戻り値がnullであった場合、サービスは開始されません。言及されたフラグはPendingIntent.FLAG_NO_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!";
}
于 2013-09-06T07:37:54.843 に答える
2

アクティビティがIntentServiceを正常に開始したかどうかを知りたいのですが。

呼び出したときにアクティビティまたはサービスのいずれにも例外が発生しない場合はstartService()IntentServiceが開始されています。

bindService()を介してIntentServiceをバインドして、実行を継続することが可能であるため

なんで?

于 2011-08-15T12:15:15.980 に答える
0

サービスが実行されているかどうかを確認するために使用する方法は次のとおりです。

  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;
    }
于 2017-11-08T09:27:34.977 に答える