私はサービスを開始するアクティビティを持っています。私の活動にアクティビティの「死」時にサービスがクラッシュする
:私のサービスで
startService(new Intent(this, MyService.class));
、ONSTART():
/* Show notification */
int icon = R.drawable.icon;
tickerText = getResources().getString(R.string.app_name);
long when = System.currentTimeMillis();
contentTitle = getResources().getString(R.string.app_name);
contentText = getResources().getString(R.string.running);
Intent notificationIntent = new Intent(this, activityClass).setAction(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);
startForeground(0, notification);
サービスは3~4分、いくつかの作業を行うタイマーを持っている、との情報をブロードキャスト可能であれば活動。アクティビティが終了すると、サービスはその作業を継続する必要があります。
私は、アプリケーションを実行し、ホーム画面に戻ると、サービスが実行を続けます。しかし、私はこれらのlogcatメッセージを取得し、サービスが停止し、しばらくして:
07-03 16:55:09.440:INFO/ActivityManager(583):プロセスcom.myapp(PID 11665)が死亡しました。 07-03 16:55:09.440:WARN/ActivityManager(583):私はこれを防ぐにはどうすればよい
5000msで墜落したサービスcom.myapp/.MyServiceのスケジュールの再起動?
さて、どのようにプロセスが死ぬのを防ぐには? – nhaarman