我有一个启动服务的 Activity 。
在我的 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): Process com.myapp (pid 11665) has died. 07-03 16:55:09.440: WARN/ActivityManager(583): Scheduling restart of crashed service com.myapp/.MyService in 5000ms
我怎样才能避免这种情况?
最佳答案
所提供的答案对我没有帮助。但是我找到了一个非常简单的方法来做到这一点,在 list 中添加
<service android:name="..." android:process=":remote" />
http://developer.android.com/guide/topics/manifest/service-element.html
目前只是测试,我的 Activity 已经被杀死,但我的服务正在运行。
关于android - 服务在 Activity 的 'death' 处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6563641/