zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    docker 构建带健康检查的redis镜像
    HP服务器 开启ILO
    [转]如何取得当前正在执行的shell脚本的绝对路径?
    解决方案:centos运行shell脚本时报“$' ': 未找到命令”
    Js控制滚动条
    json_decode时含有中文是解码问题(json_decode返回为null)
    Phaser开源2d引擎 html5游戏框架中文简介
    js 开启video全屏模式
    修改mysql用户名密码 和 PHPmysqlAdmin对应密码修改
    用javascript判断一个html元素是否存在的五种方法:
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5363486.html
Copyright © 2011-2022 走看看