Android 如何自行停止服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1886874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop service by itself?
提问by Mak Sing
I start a service in an activity then I want the service to stop itself after a while.
我在活动中启动服务,然后我希望服务在一段时间后自行停止。
I called stopSelf() in the service but it doesn't work.
我在服务中调用了 stopSelf() 但它不起作用。
How to make the service stop itself?
如何让服务自行停止?
回答by AlikElzin-kilaka
By saying "doesn't work", I guess you mean that the onDestroy()
-method of the service is not invoked.
通过说“不起作用”,我猜你的意思是onDestroy()
服务的-method 没有被调用。
I had the same problem, because I boundsome ServiceConnectionto the Service itself using the flag BIND_AUTO_CREATE. This causes the service to be kept alive until every connection is unbound.
我遇到了同样的问题,因为我使用标志BIND_AUTO_CREATE将一些ServiceConnection绑定到服务本身。这会导致服务保持活动状态,直到每个连接都解除绑定。
Once I change to use no flag (zero), I had no problem killing the service by itself (stopSelf()
).
一旦我更改为不使用标志(零),我就可以自行终止服务(stopSelf()
)。
Example code:
示例代码:
final Context appContext = context.getApplicationContext();
final Intent intent = new Intent(appContext, MusicService.class);
appContext.startService(intent);
ServiceConnection connection = new ServiceConnection() {
// ...
};
appContext.bindService(intent, connection, 0);
Killing the service (not process):
终止服务(不是进程):
this.stopSelf();
Hope that helped.
希望有所帮助。
回答by sneaker_android
By calling stopSelf()
, the service stops.
通过调用stopSelf()
,服务停止。
Please make sure that no thread is running in the background which makes you feel that the service hasn't stopped.
请确保没有线程在后台运行,这让您感觉服务没有停止。
Add print statements within your thread.
在您的线程中添加打印语句。
Hope this helps.
希望这可以帮助。
回答by tony gil
since you didnt publish your code, i cant know exactly what you are doing, but you must declare WHAT you are stopping:
由于您没有发布代码,我无法确切知道您在做什么,但您必须声明您要停止的内容:
this.stopSelf();
as in:
如:
public class BatchUploadGpsData extends Service {
@Override
public void onCreate() {
Log.d("testingStopSelf", "here i am, rockin like a hurricane. onCreate service");
this.stopSelf();
}
回答by Jayesh
If by "doesn't work" you mean the process doesn't get killed, then that's how android works. The System.exit(0)
or Process.killProcess(Process.myPid())
will kill your process. But that's not the Android way of doing things.
如果“不起作用”是指进程没有被杀死,那么这就是 android 的工作方式。该System.exit(0)
或Process.killProcess(Process.myPid())
会杀死你的进程。但这不是 Android 的做事方式。
HTH
HTH
回答by ArMo 372
stopForeground(true);
stopSelf();
回答by Vignesh VRT
To let your service to stop itself.. create a BroadcastReceiver
class.. In your service call your receiver like this..
为了让你的服务停止自己..创建一个BroadcastReceiver
类..在你的服务中像这样调用你的接收器..
In service
在役
sendBroadcast(new Intent("MyReceiver"));
In Broadcast Receiver
在广播接收器中
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.stopService(new Intent(context,NotificationService.class));
}
}
Manifest file
清单文件
<receiver
android:name="MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="MyReceiver"/>
</intent-filter>
</receiver>
回答by Arunmani
Use stopSelf() to stop a service from itself.
使用 stopSelf() 从自身停止服务。
回答by Stefano F.
I know this is an old question, but in my case (floating window as service) I had to remove the view first, and then call stopSelf()
.
我知道这是一个老问题,但就我而言(浮动窗口即服务),我必须先删除视图,然后调用stopSelf()
.
windowManager.removeView(floatingView);
stopSelf();
回答by Arya Zady
if you use separate Thread
in your service, after stopping service by calling stopSelf()
or stopService()
the Thread
keeps running. if u want to stop Thread
u should call Thread.interrupted()
in the Thread
(it might cause an Exception
if Thread
is already sleeping)
如果您使用单独Thread
的服务,通过调用停止服务后,stopSelf()
或stopService()
将Thread
保持运行。如果u要停止Thread
ü应该调用Thread.interrupted()
在Thread
(它可能会导致Exception
如果Thread
已经睡)
回答by yuliskov
Another dirty hack not mentioned here is to throw an exception like NPE. One day I needed to stop InputMethodService and this hack was useful.
这里没有提到的另一个肮脏的黑客是抛出像 NPE 这样的异常。有一天我需要停止 InputMethodService 并且这个 hack 很有用。