服务因活动"死亡"而崩溃

nha*_*man 3 crash service android

我有一个启动服务的活动.

在我的活动中:

startService(new Intent(this, MyService.class));
Run Code Online (Sandbox Code Playgroud)

在我的服务中,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);
Run Code Online (Sandbox Code Playgroud)

该服务有一个计时器,每隔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的重启

我怎么能阻止这个?

seb*_*seb 7

提供的答案对我没有帮助.但是我发现了一个非常简单的方法,添加到Manifest中

    <service android:name="..." android:process=":remote" />
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/guide/topics/manifest/service-element.html

刚刚测试,我的Activity已经被杀死,但我的服务正在运行.