1

バックグラウンドで動作するサービスがあります。彼は START_STICKY を返します。これは、いくつかのイベントをリッスンするためです。このイベントは非常に高速に発生します (センサーからのデータ)。場合によっては、サービスが強制終了されます。null インテントがあり、ドキュメントによると、プロセスがなくなったためにサービスが強制終了されたときに発生する可能性があります。

http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29

回避方法が知りたいです。どんな助けでも大歓迎です。

アップデート

    private void startForeground() {
    String title = "Warning";//getString(R.string.note_title);
    String text = "App still working";//getString(R.string.note_msg);

    Intent intent = new Intent(this, DriverActivity.class);

    PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);


    Bitmap bitmap = BitmapFactory.decodeResource(
            getResources(),
            R.drawable.ic_launcher);
    Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(bitmap)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(pending);

    Notification notification = builder.getNotification();
    notification.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(NOTIFICATION_ID, notification);
}
4

1 に答える 1

1

まず、サービスが終了した理由を特定する必要があります。最も可能性の高い動機は次の 2 つです。

  • サービスが例外をスローし、Android によって終了されました
  • フォアグラウンド サービスとして指定されずに長時間 (通常は 30 分から 1 時間) 実行されたため、サービスは Android によって終了されました

例外 このタイプの問題は、logcat を見れば簡単に特定できます。修正は、報告された原因によって異なります。

これは、次を使用してサービスをフォアグラウンド に設定することで解決できます。

        startForeground(message, notification);

あなたのサービスで。

よろしく。

于 2012-11-23T16:46:27.437 に答える