gpt4 book ai didi

Android - 如何确保我的服务重新启动?

转载 作者:行者123 更新时间:2023-11-29 22:32:12 25 4
gpt4 key购买 nike

我有一个带有后台运行服务的 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 - 如何确保我的服务重新启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3607861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com