保活(三)-前台服务

前台服务可提高进程等级,提高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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容