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

标签 android service crash restart

我有一个带有后台运行服务的 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/

相关文章:

java - 如何在 MSWord 中使用 Intent

selenium - 当我们使用 Jenkins windows 服务模式执行 selenium 脚本时,从渲染器 : 600. 000 接收消息超时

javascript - 如何确定哪种JavaScript导致浏览器崩溃?

xcode - 新创建的 swift 项目在设备中崩溃

android - 关闭应用程序时停止 AsyncTask

crash - VMR-9 无渲染模式 : SelectAtPosition() crashes

java - 在监听器外部更改 Android ListView 选定行属性

java - 数据库版本 SQLite Android

android - Phonegap 指南针插件无法正常工作

service - 用于格式化两位小数的 SSRS 表达式不显示零