android 关于提高第三方app的service优先级

摘要:
重启什么3.Android:priorityservice根本没有这个属性4.setForeground属性有效,但在线示例无效,因为参数错误可以帮助服务通过提高其重要性来避免批评。官方文件中指出,有五个级别的进展,其中前景进程最为重要。

本博客仅仅要没有注明“转”。那么均为原创,转贴请注明本博客链接链接


基本上大家都知道提高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);
上面的三个属性放到一起,值为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;
最后。我们能够使用以下命令看看手机中的哪些应用这么干了,你在平时使用的时候是不是他们存活时间最长。最不easy被系统干掉
dumpsys notification

转贴请保留以下链接

本人blog地址

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

免责声明:文章转载自《android 关于提高第三方app的service优先级》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Mysql—二进制日志(binlog)【鲲鹏来了】华为云鲲鹏弹性云服务器 KC1一文全掌握(3)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#综合揭秘——细说多线程(上)

引言   本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发。   其中委托的BeginInvoke方法以及回调函数最为常用。   而 I/O线程可能容易遭到大家的忽略,其实在开发多线程系统,更应该多留意I/O线程的操作。特别是在ASP.NET开发当中,可能更多人只会留意在客户端使用Ajax...

FreeRTOS中断优先级配置(重要)

FreeRTOS中断优先级配置(重要)    本章节为大家讲解FreeRTOS中断优先级配置,此章节非常重要,初学者经常在这里犯迷糊。对于初学者来说,本章节务必要整明白。12.1 NVIC基础知识12.2 使用FreeRTOS时如何配置外设NVIC12.3 FreeRTOS配置选项中NVIC相关配置12.4 不受FreeRTOS管理中的的深入讨论12.5总...

UCOSIII任务创建

UCOSIII任务创建: 首先要确保UCOSIII移植成功; 一般以下几个步骤就行了: 第一步:写好任务所需的 优先级、堆栈、控制块等; 然后顺便声明下任务函数; 第二步:开始创建函数 源码: 1 #include "led.h" 2 #include "delay.h" 3 #include "sys.h" 4 #include "usart.h"...

Linux-c 线程锁

1 typedef struct_my_mutex { 2 pthread_mutex_t mutex; //互斥锁 3 pthread_mutexattr_t mta; //互斥锁属性 4 } my_mutex; 转自:http://blog.sina.com.cn/s/blog_8795b0970101il6g.html 在Posix Threa...

HTTP协议探究(五):H2中一些重要的概念

一 复习与目标 1 复习 简单密码学、对称加密与非对称加密 数字签名、数字证书 SSL/TLS HTTPS = HTTP + SSL/TLS,SSL/TLS为HTTP提供了保密性、完整性和鉴别性 2 目标 HTTP1.1的问题 HTTP2.0设计关键 HTTP2.0的详情 注1:本文并不会讲解h2的所有内容,只会包含重要(主观)的内容。 注2:下一...

Preload与Prefetch的区别以及webpack项目中如何优化

preload 与prefetch 的区别 preload 是一个声明式 fetch,可以强制浏览器在不阻塞 document 的 onload 事件的情况下请求资源。 preload 顾名思义就是一种预加载的方式,它通过声明向浏览器声明一个需要提交加载的资源,当资源真正被使用的时候立即执行,就无需等待网络的消耗。 prefetch 告诉浏览器这个...