【发布时间】:2011-09-27 15:56:17
【问题描述】:
我有一个启动服务的 Activity。
在我的活动中:
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 分钟执行一次工作,并在可能的情况下向 Activity 广播信息。当 Activity 关闭时,Service 应该继续工作。
当我运行应用程序并返回主屏幕时,服务会继续运行。但过了一会儿,我收到了这些 logcat 消息,服务停止了:
07-03 16:55:09.440:INFO/ActivityManager(583):进程 com.myapp (pid 11665) 已死亡。 07-03 16:55:09.440: WARN/ActivityManager(583): 计划在 5000 毫秒内重新启动崩溃的服务 com.myapp/.MyService
如何防止这种情况发生?
【问题讨论】:
-
好的,如何防止进程死机?