android开发之关于提高第三方app的service优先级(以及例举网上常见的错误方法)
2014-08-21 09:00
766 查看
基本上大家都知道提高service优先级可以在很大程度上让你的service免于因为内存不足而被kill,当然系统只是在此时先把优先级低的kill掉,如果内存还是不够,也会把你的service干掉的。不过现在的机器不像几年前了,基本上不会发生那种情况。
先来看看网上常见的错误方法:
1.android:persistent="true"
对第三方app无效,下面是官方说明
if not. The default value is "
2.onDestroy中重启service
service被系统杀死的时候并不一定会执行onDestroy,拿什么重启
3.android:priority
service根本没有这属性
4.setForeground
这个是有效的,但是网上的例子却都是无效的原因是参数错误
让service免于非难的办法是提高它的重要性,在官方文档中已经说明进程有五个级别,其中前台进程最重要,所以最后被杀死。
如何使之变成前台进程可以参阅官方文档。
http://developer.android.com/guide/components/processes-and-threads.html
/article/3762951.html
这里只说如何使用setForeground将service设置为前台进程
上面的三个属性放到一起,值为0x62。
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification is in reference to something that is ongoing,
* like a phone call. It should not be set if this notification is in
* reference to something that happened at a particular point in time,
* like a missed phone call.
*/
public static final int FLAG_ONGOING_EVENT = 0x00000002;
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if the notification should not be canceled when the user clicks
* the Clear all button.
*/
public static final int FLAG_NO_CLEAR = 0x00000020;
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification represents a currently running service. This
* will normally be set for you by {@link Service#startForeground}.
*/
public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
最后,我们可以使用下面命令看看手机中的哪些应用这么干了,你在平时使用的时候是不是他们存活时间最长,最不容易被系统干掉
dumpsys notification
转自:
/article/1612763.html
先来看看网上常见的错误方法:
1.android:persistent="true"
对第三方app无效,下面是官方说明
android:persistentWhether or not the application should remain running at all times — "
true" if it should, and "
false"
if not. The default value is "
false". Applications should not normally set this flag; persistence mode is intended only for certain system applications.
2.onDestroy中重启service
service被系统杀死的时候并不一定会执行onDestroy,拿什么重启
3.android:priority
service根本没有这属性
4.setForeground
这个是有效的,但是网上的例子却都是无效的原因是参数错误
让service免于非难的办法是提高它的重要性,在官方文档中已经说明进程有五个级别,其中前台进程最重要,所以最后被杀死。
如何使之变成前台进程可以参阅官方文档。
http://developer.android.com/guide/components/processes-and-threads.html
/article/3762951.html
这里只说如何使用setForeground将service设置为前台进程
<span style="font-family:KaiTi_GB2312;font-size:18px;">Notification notification = new Notification(); notification.flags = Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_NO_CLEAR; notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; service.startForeground(1, notification); </span>
上面的三个属性放到一起,值为0x62。
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification is in reference to something that is ongoing,
* like a phone call. It should not be set if this notification is in
* reference to something that happened at a particular point in time,
* like a missed phone call.
*/
public static final int FLAG_ONGOING_EVENT = 0x00000002;
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if the notification should not be canceled when the user clicks
* the Clear all button.
*/
public static final int FLAG_NO_CLEAR = 0x00000020;
/**
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification represents a currently running service. This
* will normally be set for you by {@link Service#startForeground}.
*/
public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
最后,我们可以使用下面命令看看手机中的哪些应用这么干了,你在平时使用的时候是不是他们存活时间最长,最不容易被系统干掉
dumpsys notification
转自:
/article/1612763.html
相关文章推荐
- android 关于提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- Android ROM开发(二)——ROM架构以及Updater-Script脚本分析,常见的Status错误解决的方法
- [android基础知识] 之十八: android service提高自身优先级以及防止被杀的方法汇总
- android 关于提高app的进程service优先级
- (转)关于在android中service开发的IllegalArgumentException错误和服务无法启动的解释
- Android 开发常见错误及解决方法集锦
- 提高第三方app的service优先级
- Android界面开发中添加第三方字体类型以及清除失败的解决方法。
- 关于在eclipse下android开发的错误:“Unable to resolve target 'android-18'"的解决方法
- 从网上查到的Android开发常见错误及技巧
- Android ROM开发(二)——ROM架构以及Updater-Script脚本分析,常见的Status错误解决办法
- Android提高Service优先级的方法分析
- Android基础总结——进程优先级及提高优先级的方法(Service尽量不死之法)
- 有关于提高C/C++运行效率以及避免出现Bug的几种常见的实用方法
- Android开发中的9个常见错误和解决方法
- WCF分布式开发常见错误解决(6)Service 'WcfServiceApp.WCFService' has zero application
- WCF分布式开发常见错误解决(6)Service 'WcfServiceApp.WCFService' has zero application