回答
如果通过“不工作”,你的意思是这个过程不会被杀死,那么这就是android的工作原理。 System.exit(0)
或Process.killProcess(Process.myPid())
会杀死你的进程。但这不是Android做事的方式。
HTH
通过说“不行”,我猜你的意思是该服务的onDestroy()
- 方法不会被调用。
我有同样的问题,因为我bound一些ServiceConnection服务本身使用标志BIND_AUTO_CREATE。 这会导致服务保持活动状态,直到每个连接为unbound。
一旦我改变使用没有标志(零),我没有问题自己杀死服务(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();
希望有所帮助。
,因为你没有发布你的代码,我不能确切地知道你在做什么,但你必须声明你正在停止:
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();
}
纠正我,如果我错了,但不会停止自我()没有“这个”的工作,以及? – 2015-03-21 20:17:26
@AbrahamPhilip'This'是明确的,但我不能说这是绝对必要的。请尽可能测试。韩国社交协会。 – 2015-03-25 23:46:22
这不是必须的,但为了清晰起见添加它并没有什么坏处 – 2015-07-20 10:37:01
通过调用stopSelf()
,服务停止。
请确保没有线程在后台运行,这会让您觉得服务尚未停止。
在您的线程中添加打印语句。
希望这会有所帮助。
我刚碰到同样的问题。就我而言,我有一个单身服务经理,用于与服务进行通信。在管理服务已启动这样的:
context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE);
通过由阿利克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()当您需要阻止它。
stopForeground(true);
stopSelf();
这里没有提到的另一个肮脏的黑客是抛出像NPE的异常。有一天我需要停止InputMethodService,并且这个入侵很有用。
为了让你的服务停止本身..创建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));
}
}
Manifest f ILE
<receiver
android:name="MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="MyReceiver"/>
</intent-filter>
</receiver>
,如果你在你的服务使用单独的Thread
,通过调用stopSelf()
或stopService()
的Thread
保持运行停止售后服务。如果u要停止Thread
ü应该调用在Thread
Thread.interrupted()
(这可能会导致Exception
)
- 1. 如何停止运行服务?
- 2. Windows服务自动停止
- 3. 停止自定义服务
- 4. 如何停止服务?
- 5. 如何停止NPM服务
- 6. 如何停止android服务?
- 7. 如何停止服务中的服务
- 8. 如何告诉Puppet在停止运行时停止服务?
- 9. 如何让Android服务完成后自行停止?
- 10. 如何禁止停止windows服务
- 11. 如何停止作为Windows服务运行的web.py服务器
- 12. 服务 - 服务未停止
- 13. 何时以及如何停止服务
- 14. 系统服务的停止如何触发不同服务的停止?
- 15. Android停止服务
- 16. Android服务停止
- 17. 服务不停止
- 18. 停止或中止服务
- 19. Mongo DB服务自动停止
- 20. 如何停止散景服务器?
- 21. 如何停止pyftpdlib FTP服务器?
- 22. 如何停止一个heroku服务器?
- 23. 如何停止rails服务器(redmine)?
- 24. 如何立即停止服务立即
- 25. 如何停止系统服务?
- 26. 如何从活动中停止服务
- 27. 如何停止单向WCF服务
- 28. 如何从eclipse停止Jetty服务器
- 29. 如何检查服务是否停止
- 30. 如何停止WCF Web服务?
因为答案是“叫stopSelf()”,你将需要提供关于更多的细节“它好好尝试一下工作”。 – CommonsWare 2009-12-11 10:18:02
需要更多的内容,只是'“不工作”' – footy 2011-11-26 16:13:00
像一个答案?随意标记*接受* :) – 2013-01-20 12:04:55