偶尔被杀是不是大问题?你不能只检查它是否正在运行,如果没有就启动吗?要在您的活动中执行此操作:
if (!isMyServiceRunning()) {
startService();
}
...
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private void startService() {
Intent myService = new Intent(this, MyService.class);
startService(myService);
}