服务在“死亡”时崩溃;活动数量

发布于 2024-11-18 09:54:25 字数 1366 浏览 1 评论 0原文

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

在我的活动中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多像笑话 2024-11-25 09:54:25

提供的答案对我没有帮助。但我找到了一个非常简单的方法来做到这一点,添加在清单

    <service android:name="..." android:process=":remote" />

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

    <service android:name="..." android:process=":remote" />

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.

机场等船 2024-11-25 09:54:25

当我运行应用程序并返回主屏幕时,服务将继续运行。但过了一会儿我收到这些 logcat 消息,并且服务停止了......我该如何防止这种情况?

一般来说,你不会。

服务并不是为了永远运行而设计的。太多的开发人员已经开始提供服务并且从未停止过。而且,就您而言,他们这样做是没有充分理由的。让一个服务在内存中停留,只是看着时钟滴答作响是一种浪费,尤其是因为开发人员这样做,Android 服务在停留太久后就会“崩溃”。

对于您的情况,请改用 AlarmManager,可能与 IntentService 结合使用,这样当没有更多工作要做时,服务会自行关闭。

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... How can I prevent this?

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 an IntentService, so the service shuts down on its own accord when there is no more work to be done.

未央 2024-11-25 09:54:25

您将需要考虑让您的服务作为远程服务运行,以便它在自己的进程中运行。

关于远程服务的博客:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文