Android的 - 如何确保我的服务重新启动? [英] Android - how to make sure my Service is restarted?
问题描述
我有一个后台运行的服务的Android应用程序。
I have an Android application with a background running Service.
在服务崩溃或由Android打死我可以看到,Android的尝试再次重新启动。
When the Service crashes or gets killed by Android I can see that Android tries to restart it again.
但是服务实际上从未重新启动,我可以看到Android的调度重启,但它实际上新发生的。
However the Service never actually restarts, I can see Android scheduling the restart but it new actually happens.
我的code是如下:
@Override
public void onCreate() {
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
void handleCommand(Intent intent) {
running = true;
mTelephonyManager.listen(new IncomingListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
有东西在我的code缺失,使服务重新启动?
Is there something missing in my code to allow the Service be restarted?
推荐答案
您是否尝试过使用 startForeground()
<一个href=\"http://developer.android.com/intl/de/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29\" rel=\"nofollow\">http://developer.android.com/intl/de/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29
这篇关于Android的 - 如何确保我的服务重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!