Android Service被系统回收的解决方法

转自:http://www.bdqn.cn/news/201306/9554.shtml


使用的前提是调用startService而不是bindService

自己的app的service总是容易被系统回收,搜罗了一下,基本上的解决思路有以下几种:

1.把service写成系统服务,将不会被回收(未实践):

在Manifest.xml文件中设置persistent属性为true,则可使该服务免受out-of-memory killer的影响。但是这种做法一定要谨慎,系统服务太多将严重影响系统的整体运行效率。

2.提高service的优先级(未实践):

设置android:priority="1000"

Xml代码

1
2
3
4
5
<!-- 为了消去加上android:priority="1000"后出现的警告信息,可以设置android:exported属性,指示该服务是否能够被其他应用程序组件调用或跟它交互 -->
     < service android:name = "com.example.helloandroid.weatherforecast.service.UpdateWidgetService" android:exported = "false"  >
  <!-- 为防止Service被系统回收,可以通过提高优先级解决,1000是最高优先级,数字越小,优先级越低 -->
      < intent-filter  android:priority = "1000" ></ intent-filter >
      </ service >


3.将服务写成前台服务foreground service(已实践,很大程度上能解决问题,但不能保证一定不会被杀):

重写onStartCommand方法,使用StartForeground(int,Notification)方法来启动service。

注:前台服务会在状态栏显示一个通知,最典型的应用就是音乐播放器,只要在播放状态下,就算休眠也不会被杀,如果不想显示通知,只要把参数里的int设为0即可。

Java代码

1
2
3
4
5
6
7
8
9
10
Notification notification =  new  Notification(R.drawable.logo,
                 "wf update service is running" ,
                 System.currentTimeMillis());
         pintent=PendingIntent.getService( this 0 , intent,  0 );
         notification.setLatestEventInfo( this "WF Update Service" ,
                 "wf update service is running!" , pintent);
            
         //让该service前台运行,避免手机休眠时系统自动杀掉该服务
         //如果 id 为 0 ,那么状态栏的 notification 将不会显示。
         startForeground(startId, notification);

同时,对于通过startForeground启动的service,onDestory方法中需要通过stopForeground(true)来取消前台运行状态。

ps:如果service被杀后下次重启出错,可能是此时重发的Intent为null的缘故,可以通过修改onStartCommand方法的返回值来解决:

START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。

START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。

START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。

START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

Java代码

1
2
3
4
5
6
7
8
9
10
Notification notification =  new  Notification(R.drawable.logo,
                 "wf update service is running" ,
                 System.currentTimeMillis());
         pintent=PendingIntent.getService( this 0 , intent,  0 );
         notification.setLatestEventInfo( this "WF Update Service" ,
                 "wf update service is running!" , pintent);
            
         //让该service前台运行,避免手机休眠时系统自动杀掉该服务
         //如果 id 为 0 ,那么状态栏的 notification 将不会显示。
         startForeground(startId, notification);


4.利用ANDROID的系统广播检查Service的运行状态,如果被杀掉,就再起来(未实践):

利用的系统广播是Intent.ACTION_TIME_TICK,这个广播每分钟发送一次,我们可以每分钟检查一次Service的运行状态,如果已经被结束了,就重新启动Service。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值