使用onstart命令后如何停止服务?

时间:2012-10-02 15:06:31

标签: android

使用onStartCommand方法时如何让我的服务停止?

我知道我必须使用stopService()但是Eclipse给了我一个错误。

到目前为止我有这个代码:

    package be.pegus.classapp;

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;

    public class ClassService extends Service {
    private static String tag = "something";
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Log.d(tag, "k");
    }



@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.d(tag, "destroy");
}

1 个答案:

答案 0 :(得分:2)

您可以尝试在 onStartCommand 方法中调用stopSelf();来完成您的服务。这不需要您提供用于服务的Intent