服务在“死亡”时崩溃;活动数量
我有一个启动服务的活动。
在我的活动中:
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 关闭时,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
我怎样才能防止这种情况?
I have an Activity that starts a Service.
In my Activity:
startService(new Intent(this, MyService.class));
In my Service, 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);
The service has a timer that does some work every 3-4 minutes, and broadcasts info to the Activity if possible. When the Activity is closed, the Service should keep doing its work.
When I run the app, and go back to the homescreen, the Service keeps running. But after a while I get these logcat messages, and the service stops:
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
How can I prevent this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提供的答案对我没有帮助。但我找到了一个非常简单的方法来做到这一点,添加在清单
http ://developer.android.com/guide/topics/manifest/service-element.html
目前正在测试,我的 Activity 已经被终止,但我的服务正在运行。
The provided answers didn't help me. But I found a really easy way to do this, add in the Manifest
http://developer.android.com/guide/topics/manifest/service-element.html
Just testing at the moment, my Activity got already killed, but my service is running.
一般来说,你不会。
服务并不是为了永远运行而设计的。太多的开发人员已经开始提供服务并且从未停止过。而且,就您而言,他们这样做是没有充分理由的。让一个服务在内存中停留,只是看着时钟滴答作响是一种浪费,尤其是因为开发人员这样做,Android 服务在停留太久后就会“崩溃”。
对于您的情况,请改用
AlarmManager
,可能与IntentService
结合使用,这样当没有更多工作要做时,服务会自行关闭。You don't, generally speaking.
Services are not designed to run forever. Too many developers have started services and never stopped them. And, in your case, they do so for no good reason. Having a service hang around in memory just watching the clock tick is wasteful, and it is particularly because developers do things like this that Android "crashes" services after they stick around too long.
In your case, please switch to use
AlarmManager
, probably in conjunction with anIntentService
, so the service shuts down on its own accord when there is no more work to be done.您将需要考虑让您的服务作为
远程服务
运行,以便它在自己的进程中运行。关于远程服务的博客:
http://saigeethamn.blogspot.com/2009/09 /android-developer-tutorial-part-9.html
示例示例:
http://about-android.blogspot.com/2010 /02/android-remote-service-sample.html
You will want to look at having your service run as a
Remote Service
, so that it runs in its own process.A blog about remote services:
http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html
An example of a sample one:
http://about-android.blogspot.com/2010/02/android-remote-service-sample.html