我有一个后台运行Service的Android应用程序。

当服务崩溃或被Android杀死时,我可以看到Android尝试重新启动它。

但是,该服务从未真正重启,我可以看到Android安排了重启,但实际上是新的。

我的代码如下:

@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);

    }

我的代码中是否缺少允许重新启动服务的内容?

最佳答案

您是否尝试过使用 startForeground()

关于android - Android-如何确保我的服务重新启动?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3607861/

10-11 22:25
查看更多