防止Android Service被系统回收的4个方法

Home / Android MrLee 2015-4-21 5197

自己写的App在后台时间久了很可能被回收,那么怎么处理这个问题呢?
搜罗了一下,基本上的解决思路有以下几种:
1.把service写成系统服务,将不会被回收(未实践):
在Manifest.xml文件中设置persistent属性为true,则可使该服务免受out-of-memory killer的影响。但是这种做法一定要谨慎,系统服务太多将严重影响系统的整体运行效率。
2.提高service的优先级(未实践):
设置android:priority="1000"
Xml代码

    
 
     
     

3.将服务写成前台服务foreground service(已实践,很大程度上能解决问题,但不能保证一定不会被杀):
重写onStartCommand方法,使用StartForeground(int,Notification)方法来启动service。
注:前台服务会在状态栏显示一个通知,最典型的应用就是音乐播放器,只要在播放状态下,就算休眠也不会被杀,如果不想显示通知,只要把参数里的int设为0即可。
Java代码
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代码
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。

本文链接:https://www.it72.com/2274.htm

推荐阅读
最新回复 (0)
返回