1

I have an Android application with a background running Service.

When the Service crashes or gets killed by Android I can see that Android tries to restart it again.

However the Service never actually restarts, I can see Android scheduling the restart but it new actually happens.

My code is as follows:


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

    }

Is there something missing in my code to allow the Service be restarted?

Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191

1 Answers1

2

Have you tried using startForeground()?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • Yes I implemented that and it gave me some very strange behaviour, it popped up the notification even when the app and Service should not have been running. The thing is I tried with an empty onStart() method and the Service seemed to restart fine. – Donal Rafferty Aug 31 '10 at 10:03