美文网首页
保活(三)-前台服务

保活(三)-前台服务

作者: 火星局 | 来源:发表于2018-05-29 17:13 被阅读0次

前台服务可提高进程等级,提高App进程存活性

1. 服务

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.xxxx.xxxx.R;
import com.orhanobut.logger.Logger;

/**
 * 描述:
 * <p>
 *
 * @author allens
 * @date 2018/1/23
 */

public class KeepLiveService extends Service {
    public static final int NOTIFICATION_ID = 0x11;

    public KeepLiveService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.e("KeepLiveService  onCreate");
        //API 18以下,直接发送Notification并将其置为前台
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            startForeground(NOTIFICATION_ID, new Notification());
        } else {
            //API 18以上,发送Notification并将其置为前台后,启动InnerService
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.water_icn)
                    //设置通知标题
                    .setContentTitle("xxx系统");
            startForeground(NOTIFICATION_ID, builder.build());
            startService(new Intent(this, InnerService.class));
        }
    }

    public static class InnerService extends Service {
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Logger.e("InnerService onCreate");
            //发送与KeepLiveService中ID相同的Notification,然后将其取消并取消自己的前台显示
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            startForeground(NOTIFICATION_ID, builder.build());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    stopForeground(true);
                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    manager.cancel(NOTIFICATION_ID);
                    stopSelf();
                }
            }, 100);
        }
    }
}

2.注册服务

     <service android:name="com.xxx.xxxx.service.KeepLiveService" />
    <service android:name="com$xxx$xxxx$service$KeepLiveService$InnerService" />

3.查看adj级别

  • 8adb shell

  • ps|grep <package_name> 查看基本信息

1|root@generic_x86:/ # ps|grep com.cpsc.livedemo                               
u0_a63    6834  1348  1285208 43884 SyS_epoll_ b73712b5 S com.xxxx.livedemo
u0_a63    6884  1348  1271160 28944 SyS_epoll_ b73712b5 S com.xxxx.livedemo:daemon_service
解释
u0_a63 USER 进程当前用户
6834 进程ID
1348 进程的父进程ID
1285208 进程的虚拟内存大小
43884 实际驻留”在内存中”的内存大小
com.cpsc.livedemo 进程名
  • cat /proc/<进程id>/oom_adj
root@generic_x86:/ # cat /proc/6884/oom_adj                                    
1
root@generic_x86:/ # cat /proc/6884/oom_adj                                    
1
root@generic_x86:/ # 
adj级别 说明
UNKNOWN_ADJ 16 预留的最低级别,一般对于缓存的进程才有可能设置成这个级别
CACHED_APP_MAX_ADJ 15 缓存进程,空进程,在内存不足的情况下就会优先被kill
CACHED_APP_MIN_ADJ 9 缓存进程,也就是空进程
SERVICE_B_ADJ 8 不活跃的进程
PREVIOUS_APP_ADJ 7 切换进程
HOME_APP_ADJ 6 与Home交互的进程
SERVICE_ADJ 5 有Service的进程
HEAVY_WEIGHT_APP_ADJ 4 高权重进程
BACKUP_APP_ADJ 3 正在备份的进程
PERCEPTIBLE_APP_ADJ 2 可感知的进程,比如那种播放音乐
VISIBLE_APP_ADJ 1 可见进程
FOREGROUND_APP_ADJ 0 前台进程
PERSISTENT_SERVICE_ADJ -11 重要进程
PERSISTENT_PROC_ADJ -12 核心进程
SYSTEM_ADJ -16 系统进程
NATIVE_ADJ -17 系统起的Native进程

参考:
https://blog.csdn.net/andrexpert/article/details/75045678

相关文章

  • 保活(三)-前台服务

    前台服务可提高进程等级,提高App进程存活性 1. 服务 2.注册服务 3.查看adj级别 8adb shell ...

  • 进程保活与拉活

    进程相关知识梳理 Activity 1像素保活 前台服务保活 账户同步拉活 JobScheduler 拉活 双进程...

  • Android - 保活(1)前台服务保活

    前言 项目中遇到一个需求,需要竟可能的上传用户的定位信息,引发了我对目前已知的保活手段的探究,同时也遇到过客户说,...

  • Android -- 进程保活

    提供几个进程保活技术:1像素Activity,前台服务,账号同步,Jobscheduler,相互唤醒,系统服务捆绑...

  • Android前台服务保活

    现状 由于各种App后台常驻,互相唤醒的泛滥,google越来越严格的限制了App在后台长时间活动。在高版本and...

  • 服务保活

    一、概述 首先介绍一下Service的等级:一、前台进程二、可见进程三、服务进程四、后台进程五、空进程 ---关...

  • 保活 (一)- Job Service ,JobSchedule

    直接上代码 清单文件 为了提高app的保活可能性,项目中使用1px像素Activity ,前台服务,播放无声音乐等...

  • Android 服务的限制

    服务的分类 Google官网将Android服务分为了三种,前台服务,后台服务和绑定服务: 前台 前台服务执行一些...

  • Android进程保活(二):利用 Notification 提

    前一篇文章 —— Android进程保活(一):利用 Activity 提升权限讲到了利用前台的 Activity...

  • Android 进程保活系列:(二)利用 Notificatio

    前一篇文章 —— Android 进程保活系列:(一)利用 Activity 提升权限 讲到了利用前台的 Acti...

网友评论

      本文标题:保活(三)-前台服务

      本文链接:https://www.haomeiwen.com/subject/yeuajftx.html