我有一个主类,有两个按钮来启动和停止服务。
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");
}
请帮助如何杀死此服务。
答案 0 :(得分:0)
在此示例中尝试使用停止服务stopService(itnt_BackServices );
itnt_BackServices
是服务名称。你不能停止服务类中的服务,但停止任何活动。
答案 1 :(得分:0)
尝试将此添加到您的服务中
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.startId = startId;
return mStartMode;
}
@Override
public void onDestroy() {
stopSelf(startId);
}