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

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

大家好,又见面了,我是全栈君。

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


基本上大家都知道提高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/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115439.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java高并发解决方案

    Java高并发解决方案电商的秒杀和抢购,对我们来说,都不是一个陌生的东西。然而,从技术的角度来说,这对于Web系统是一个巨大的考验。当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要。这次我们会关注秒杀和抢购的技术实现和优化,同时,从技术层面揭开,为什么我们总是不容易抢到火车票的原因?一、大规模并发带来的挑战在过去的工作中,我曾经面对过5w每秒的高并发秒杀功能,在这个过程中,整个W…

    2022年5月31日
    27
  • SingleTask启动模式与HOME键问题

    SingleTask启动模式与HOME键问题Android中Activity启动模式SingleTask时点击Home键问题:http://blog.csdn.net/java201159416/article/details/51992249

    2022年6月26日
    23
  • setdefault函数的用法及理解

    setdefault函数的用法及理解dict.setdefault(key,default=None)功能:如果键不存在于字典中,将会添加该键并将default的值设为该键的默认值,如果键存在于字典中,将读出该键原来对应的值,default的值不会覆盖原来已经存在的键的值。参数:key—-要查找的键default—–查找的键不存在时用于设置的默认值使用方法示例:(以下使用方法是我理解setdefault函…

    2025年7月13日
    0
  • 解决‘E: 无法获得锁 /var/lib/dpkg/lock-frontend – open (11: 资源暂时不可用) ’ 问题[通俗易懂]

    解决‘E: 无法获得锁 /var/lib/dpkg/lock-frontend – open (11: 资源暂时不可用) ’ 问题[通俗易懂]报错无法获得锁/var/lib/dpkg/lock-frontend-open(11:资源暂时不可用)首先查看下ls/var/lib/dpkg/lock-frontend然后删除sudo-r-f/var/lib/dpkg/lock-frontend如果依然报错无法获得锁/var/lib/dpkg/lock-open(11:资源暂时不可用)’删…

    2022年10月7日
    0
  • Vue和vue全家桶有什么区别_Vue和vue全家桶有什么区别

    Vue和vue全家桶有什么区别_Vue和vue全家桶有什么区别vue全家桶都有什么全家桶,顾名思义,对于开发一个完整的中大型单页面应用项目所必须的所必须的插件和框架。注:此文章主要讲解vue-cli脚手架开发方式,不介绍各插件的具体使用方式,具体使用方式详见其他详细介绍文章。一、vue-clivue-cli也叫脚手架,官方定义为Vue.js开发的标准工具!相比scirpt标签引入,脚手架具有如下特点:1)、功能丰富对Babel、TypeScr…

    2025年7月24日
    1
  • mysql报错 1142 – SELECT command denied to user ‘root_ssm’@’localhost’ for table ‘user’「建议收藏」

    mysql报错 1142 – SELECT command denied to user ‘root_ssm’@’localhost’ for table ‘user’「建议收藏」错误信息的字面意思是:表“user”拒绝用户“root_ssm”@“localhost”的命令,很明显用户没有权限,那就给他授权呗!一、使用客户端授权(NavicatPremium12)1、使用root用户登陆mysql2、新建查询,输入指令:usemysql;select*fromuserwhereuser=’root_ssm’;SQL语句wh…

    2022年10月1日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号