我在一项活动中启动服务,然后我希望服务在一段时间后停止运行。
我在服务中调用了stopSelf()但它不起作用。
如何让服务自行停止?
答案 0 :(得分:61)
通过说“不起作用”,我想你的意思是不调用服务的onDestroy()
- 方法。
我遇到了同样的问题,因为bound使用标记ServiceConnection向服务本身BIND_AUTO_CREATE了一些unbound。 这会导致服务保持活动状态,直到每个连接都为{{3}}。
一旦我改为使用无标志(零),我就可以自行查杀服务(stopSelf()
)。
示例代码:
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);
杀死服务(不是处理):
this.stopSelf();
希望有所帮助。
答案 1 :(得分:19)
通过致电stopSelf()
,服务停止。
请确保后台没有正在运行的线程,这会让您觉得该服务尚未停止。
在你的帖子中添加打印语句。
希望这有帮助。
答案 2 :(得分:5)
由于你没有发布你的代码,我不确切知道你在做什么,但你必须声明你要停止的是什么:
this.stopSelf();
如:
public class BatchUploadGpsData extends Service {
@Override
public void onCreate() {
Log.d("testingStopSelf", "here i am, rockin like a hurricane. onCreate service");
this.stopSelf();
}
答案 3 :(得分:4)
如果“不起作用”你的意思是这个过程不会被杀死,那就是android的工作方式。 System.exit(0)
或Process.killProcess(Process.myPid())
会终止您的流程。但这不是Android的做事方式。
HTH
答案 4 :(得分:3)
stopForeground(true);
stopSelf();
答案 5 :(得分:1)
我知道这是一个老问题,但是就我而言(作为服务的浮动窗口),我必须先删除视图,然后再调用stopSelf()
。
windowManager.removeView(floatingView);
stopSelf();
答案 6 :(得分:0)
我刚遇到同样的问题。就我而言,我有一个单身服务管理器,用于与服务进行通信。在管理器中,服务启动如下:
context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE);
通过删除Alik Elzin建议的Context.BIND_AUTO_CREATE,我已经能够使用this.stopSelf()停止服务,并在执行此操作时调用onDestroy()。这个问题是,之后我无法使用上面的命令从管理器重启服务。
最后,我通过使用服务的回调来解决这个问题,该回调告诉经理停止服务。这样,经理在启动/停止服务时总是负责,一切似乎都能正常工作。我不知道这样做是否有任何反指示。
代码非常简单。在服务中创建一个回调,并在连接类中将其设置在管理器中:
private ServiceConnection mServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
myService = ((MyService.LocalBinder)service).getService();
myService.setCallback(new MyService.MyServiceCallback() {
@Override
public void onStop() {
stopService();
}
});
}
public void onServiceDisconnected(ComponentName className) {
myService = null;
}
};
并停止服务:
public void stopService()
{
if(mServiceConnection != null){
try {
mContext.unbindService(mServiceConnection);
} catch (Exception e) {}
}
mContext.stopService(new Intent(mContext, BleDiscoveryService.class));
}
在服务中,只需在需要停止时调用myCallback.onStop()即可。
答案 7 :(得分:0)
这里没有提到的另一个肮脏的黑客是抛出像NPE这样的异常。有一天我需要停止InputMethodService,这个hack非常有用。
答案 8 :(得分:0)
让您的服务自行停止..创建一个BroadcastReceiver
课程..在您的服务中,请像这样给您的接收者打电话..
正在使用
sendBroadcast(new Intent("MyReceiver"));
在广播接收器中
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.stopService(new Intent(context,NotificationService.class));
}
}
清单文件
<receiver
android:name="MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="MyReceiver"/>
</intent-filter>
</receiver>
答案 9 :(得分:0)
如果您在服务中使用单独的Thread
,则在致电stopSelf()
或stopService()
停止服务后,Thread
会一直运行。如果您想停止Thread
,请拨打Thread.interrupted()
中的Thread
(如果Exception
正在睡觉,可能会导致Thread
答案 10 :(得分:0)
使用stopSelf()从自身停止服务。