1

バックグラウンドでサービスを実行している Android アプリケーションがあります。

Service が 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);

    }

サービスを再起動できるようにするために、コードに不足しているものはありますか?

4

1 に答える 1

2

使ってみましたstartForeground()か?

于 2010-08-31T09:55:08.707 に答える