1

我有一个主类,有两个按钮来启动和停止服务。

public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
        Log.d(TAG, "onClick: starting srvice");
        myService = new Intent(this, MyService.class);
        startService(myService);
        break;
    case R.id.buttonStop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(myService);
        break;
    }
}

启动服务旨在将当前 GPS 坐标发送到服务器。它开始完美,但没有在按钮点击时停止。

在 MYService.java 我有这个代码 onDestroy 方法。

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

请帮助如何杀死此服务。

4

2 回答 2

0

尝试将此添加到您的服务中

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    this.startId = startId;
    return mStartMode;
}

@Override
public void onDestroy() {
    stopSelf(startId);
}
于 2011-09-20T07:35:39.887 回答
0

尝试这个停止服务stopService(itnt_BackServices );在这个例子itnt_BackServices中是服务的名称。您不能停止服务类中的服务,而是停止任何活动。

于 2011-09-20T07:14:36.627 回答