Android Service被系统回收的解决方法
来源:互联网 发布:ftp控制协议端口 编辑:程序博客网 时间:2024/05/16 07:27
自己的app的service总是容易被系统回收,搜罗了一下,基本上的解决思路有以下几种:
1.把service写成系统服务,将不会被回收(未实践):
在Manifest.xml文件中设置persistent属性为true,则可使该服务免受out-of-memory killer的影响。但是这种做法一定要谨慎,系统服务太多将严重影响系统的整体运行效率。
2.提高service的优先级(未实践):
设置android:priority="1000"
Xml代码
<!-- 为了消去加上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代码
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。
- Android Service被系统回收的解决方法
- Android Service被系统回收的解决方法
- android service被系统回收的解决方法
- Android Service被系统回收的解决方法
- Android Service被系统回收的解决方法
- Android Service被系统回收的解决方法
- android service被系统回收的解决方法
- Android Service被系统回收的解决方法
- android service被系统回收的解决方法
- Android Service不被系统回收
- Android 防止Service被系统回收
- Android 防止Service被系统回收
- android app被系统回收后重启异常的解决方法
- 防止Android应用中的Service被系统回收
- 防止android service 被回收
- Android之如何避免系统回收service与提升service的级别
- 提高Service优先级,避免被系统回收
- 服务(Service)全解析(六)--避免被系统回收的Service
- 命令行开启WIFI
- 泰克模拟示波器2465B维修
- C++构造函数详解及显式调用构造函数
- 【树形DP】wikioi 1163 访问艺术馆
- note : IRP hook on R0
- Android Service被系统回收的解决方法
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- 编写断点续传和多线程下载模块
- 关于 javascript event flow 的一个bug
- 用Winsock实现语音全双工通信
- eclipse设置编辑xml文件的智能提示
- Transition Effects
- android 技巧集锦
- django html