在活动的“死”服务崩溃 [英] Service crashes on 'death' of Activity

查看:100
本文介绍了在活动的“死”服务崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我的活动:

  startService(新意图(这一点,MyService.class));

在我的服务,在onStart():

  / *显示通知* /
INT图标= R.drawable.icon;
tickerText = getResources()的getString(R.string.app_name)。
时长= System.currentTimeMillis的();
contentTitle = getResources()的getString(R.string.app_name)。
contentText = getResources()的getString(R.string.running)。意图notificationIntent =新意图(这一点,activityClass).setAction(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(在此,0,notificationIntent,0);
通知=新的通知(图标,tickerText时);
notification.setLatestEventInfo(这一点,contentTitle,contentText,contentIntent);notification.flags = Notification.FLAG_ONGOING_EVENT;((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE))通知(0,通知);startForeground(0,通知);

该服务有一个计时器,做了一些工作,每3-4分钟,广播信息的活动如果可能的话。当活动被关闭,该服务应继续做其工作。

当我运行应用程序,并返回到主屏幕,该服务继续运行。但过了一段时间我得到这些logcat的消息,并且该服务停止:


  

16 07-03:55:09.440:信息/ ActivityManager(583):进程com.myapp(PID 11665)已经死亡。
  07-03 16:55:09.440:WARN / ActivityManager(583):坠毁服务com.myapp调度重启/ .MyService在5000毫秒


我如何prevent呢?


解决方案

  

当我运行应用程序,并返回到主屏幕,该服务继续运行。但过了一段时间我得到这些logcat的消息,并且该服务停止......我怎么能prevent呢?


您不这样做,一般来说。

服务不是为了永远运行。太多的开发者已经开始服务,并从未停止过他们。而且,在你的情况下,他们没有很好的理由这样做。有一个服务流连在内存只是看着时钟滴答声是一种浪费,这是特别是因为开发商做这样的事情,他们坚持Android的崩溃后服务周围太长。

在你的情况,请切换到使用 AlarmManager ,很可能在与 IntentService ,所以服务关闭一起向下自行当有需要做没有更多的工作。

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?

解决方案

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.

这篇关于在活动的“死”服务崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆