我有一个后台运行服务的 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);
}
我的代码中是否缺少某些内容以允许重新启动服务?