如何杀死服务

时间:2016-06-20 20:23:00

标签: java android android-asynctask android-service

在我的项目中,我使用的是一个在onStartCommand函数中返回START_STICKY的服务。 我使用该服务在asynctask中运行一些后台处理(我从onStartCommand函数调用)。 我需要终止服务,因为当我检查手机上的应用程序设置时它会继续运行。并设置AlarmManager在X小时后再次调用服务。

我试过了

if (isMyServiceRunning()) {
   stopService(new Intent(context, MFilterIt.class));
   }



private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {

        if ("com.sixdee.mfilterit.MFilterIt".equals(service.service
                .getClassName())) {

            return true;
        }
    }
    return false;
}

我在我的asynctask中调用它。所以我看到这个行为。如果我在AlarmManager中设置的延迟是2.延迟变为6(2 * 3)。我只有当我在上面查杀服务时才会看到这种行为提到的时尚

还有其他方式或我做错了吗。

0 个答案:

没有答案
相关问题