Android服务没有停止
我有一个主类,有两个按钮来启动和停止服务。
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");
}
请帮忙如何杀死这个服务。
I have a main class with two buttons to start and stop the service.
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;
}
}
Start Service intended to send current GPS coordinates to the server. it starts perfectely but did not stop on button click.
In MYService.java i have this code onDestroy method.
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
Please help how to kill this Service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用此方法停止服务
stopService(itnt_BackServices );
在此示例中itnt_BackServices
是服务名称。您不能停止服务类中的服务,但可以停止任何活动。try this for stop services
stopService(itnt_BackServices );
in this exampleitnt_BackServices
is name of services. you cant stop services in services class but stop in any activity.尝试将其添加到您的服务中
Try adding this to your service