提高第三方app的service优先级
来源:互联网 发布:node forever windows 编辑:程序博客网 时间:2024/05/21 21:42
1、提升服务的优先级
<service android:name=".push.PushService">
</service>
Intentintent = new Intent();
intent.setAction("com.xsl.push");
context.startService(intent );
2、在Android AndroidManifest.xml的application标签中添加android:persistent属性(把service写成系统服务)
<application
</application>
切记,这个不可滥用,系统中用这个的service,app一多,整个系统就完蛋了。
3、在Service的onDestroy()中重启Service.这种方式,用户在无法再设置-运行的服务中将此服务停止。
publicvoid onDestroy() {
}
==============================================================
基本上大家都知道提高service优先级可以在很大程度上让你的service免于因为内存不足而被kill,当然系统只是在此时先把优先级低的kill掉,如果内存还是不够,也会把你的service干掉的。不过现在的机器不像几年前了,基本上不会发生那种情况。
先来看看网上常见的错误方法:
1.android:persistent="true"
对第三方app无效,下面是官方说明:
android:persistent
- Whether 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
http://su1216.iteye.com/blog/1591699
这里只说如何使用setForeground将service设置为前台进程。
- 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);
- /**
- * 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
==============================================================
- 提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- android 关于提高第三方app的service优先级
- android开发之关于提高第三方app的service优先级(以及例举网上常见的错误方法)
- android 关于提高app的进程service优先级
- 提高Android Service 优先级的方法
- 提高Android Service 优先级的方法 .
- android中如何提高service的优先级
- Android Service提高优先级
- Service优先级提高
- service如何保证不被第三方app杀死
- 启动第三方的Activity,service
- 提高service的进程优先级的方法之一,startForeground
- 常用第三方APP 的url scheme
- APP使用的一些第三方库
- 调用第三方APP
- 从第三方app返回自己的app
- curl模拟登陆
- linux下定时执行php脚本
- jquery获取当前日期的方法
- Java获取随机数的3种方法
- gets、puts、fgets、fputs
- 提高第三方app的service优先级
- 每个架构师都应该研究下康威定律
- 基于TP类的插入数据库操作
- Java内存管理—虚拟机运行时数据区域
- Java中数值类型之间的转换
- 关于VFS文件系统中的superblock、inode、d_entry和file数据结构
- 海思hi3516C+imx122板子,VLC直播或录像mp4后回放,声音都是一顿一顿的。
- 最大后验概率(MAP)- maximum a posteriori
- HTML/CSS行内元素、块元素、空(void)元素、盒模型