为什么我的服务无法销毁

时间:2013-05-23 07:47:14

标签: android service

抱歉我的英文。

我有一个Activity调用一个服务,我想从该Activity停止此服务。

我尝试使用stopService(getApplicationContext(),TempService.class)并且它不起作用。

这是我的服务代码

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("Debug", "Service start");
    mgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 

    temp = mgr.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);

    mgr.registerListener(this, temp, SensorManager.SENSOR_DELAY_FASTEST);



    return START_STICKY;
}


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
return null;
}

public void onDestroy() {
Log.d("DEBUG", "on destroy");
notificationManager.cancel(0);
super.onDestroy();  
}

在我的Activity中:boolean t总是返回false值。 我不知道。

private void processStopService(final String tag) {
    Intent intent = new Intent(getApplicationContext(), TempService.class);
    intent.addCategory(tag);
    boolean t = stopService(intent);
    if (t)
    {
        Log.e("DEBUG", "TRUE");
    }
    else
        Log.e("DEBUG", "FALSE");
}

1 个答案:

答案 0 :(得分:0)

不要将类别与服务一起使用。删除addCategory()上的Intent来电。