美文网首页
进程保活

进程保活

作者: 田间小鹿 | 来源:发表于2017-07-19 17:33 被阅读0次

Service

service:是一个后台服务,专门用来处理常驻后台的工作的组件。

Service被强的原因:

  1. 内存不足
  2. 三方软件清理进程
  3. 各大Rom厂商进行清理工作

进程被强的机制(LowmemoryKiller的工作机制)

LowmemoryKiller会在内存不足的时候扫描所有的用户进程,找到不是太重要的进程杀死。

static short lowmem_adj[6] = {
    0,
    1,
    6,
    12,
};
static int lowmem_adj_size = 4;

static int lowmem_minfree[6] = {
    3 * 512,    
    2 * 1024,    
    4 * 1024,   
    16 * 1024,   
};
static int lowmem_minfree_size = 4;
  • lowmem_adj中各项数值代表阈值的警戒级数,lowmem_minfree代表对应级数的剩余内存
  • LowmemoryKiller就是根据当前系统的可用内存多少来获取当前的警戒级数,如果进程的oom_adj大于警戒级数并且占内存最大,将会被优先杀死
  • 具有相同omm_adj的进程,则杀死占用内存较多的

根据原理可以进行改进方案:

  1. 提高进程的优先级,其实就是减小进程的p->oomkilladj(越小越重要),如启动Service调用startForeground()尽量提高进程的优先级
  2. 当应用退到后台适当释放资源然后降低APP的内存占用量,因为在oom_adj相同的时候,会优先干掉内存消耗大的进程
    3.对于要一直在后台运行的Service我们一定要轻

进程优先级

进程的重要性:优先级(越往后越容易被系统杀死)

1.前台进程:Foreground process

用户当前操作所必需的进程。如果一个进程满足以下任一条件,即视为前台进程:

  1. 用户正在交互的activity(onResume())
  2. 当某个service绑定正在交互的activity
  3. 被主动调用为前台的service(setForeground())
  4. 组件在在执行生命周期的回调(oncrete(),onStart(),OnDestroy)
  5. BroadcastReceiver正在执行onReceive()
2、可见进程: visible process
  1. 我们的activ处在onPause(),没有进入onStop()
  2. 绑定到前台的service
3.服务进程:service process

简单的startservice

4.后台进程: Backgroud process

对于用户没有直接影响的进程

android:process:":"

5.空进程 : Empty process

不含任何活动的组件(为了第二次启动更快,采用的一个权衡)

任何提升进程的优先级

  1. QQ采用在锁屏的时候启动一个1像素的activity,当用户解锁以后将这个Axtivity结束掉(顺便同时把自己的核心服务在开启一次)
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "KeepLiveActivity----onCreate!!!");
        
        Window window = getWindow();
        window.setGravity(Gravity.LEFT|Gravity.TOP);
        LayoutParams params = window.getAttributes();
        params.height = 1;
        params.width = 1;
        params.x = 0;
        params.y = 0;
        
        window.setAttributes(params);
        
        KeepLiveActivityManager.getInstance(this).setKeepLiveActivity(this);
    }

这就是1像素的布局,但是必须设置style,不然背景会是黑色的

<style name="KeepLiveStyle">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowNoDisplay">false</item>
    </style>

开锁管理类

public class KeepLiveActivityManager {
    private static KeepLiveActivityManager instance;
    private Context context;
    private WeakReference<Activity> activityInstance;

    public static KeepLiveActivityManager getInstance(Context context) {
        if(instance==null){
            instance = new KeepLiveActivityManager(context.getApplicationContext());
        }
        return instance;
    }
    
    private KeepLiveActivityManager(Context context) {
        this.context = context;
    }
    
    public void setKeepLiveActivity(Activity activity){
        activityInstance = new WeakReference<Activity>(activity);
    }

    public void startKeepLiveActivity() {
        Intent intent = new  Intent(context, KeepLiveActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public void finishKeepLiveActivity() {
        if(activityInstance!=null&&activityInstance.get()!=null){
            Activity activity = activityInstance.get();
            activity.finish();
        }
    }
}

监听锁屏

Intent intent = new Intent(this, MyService.class);
startService(intent);
public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        ScreenListener listener = new ScreenListener(this);
        listener.begin(new ScreenStateListener() {
            
            @Override
            public void onUserPresent() {
            }
            
            @Override
            public void onScreenOn() {
                // 开屏---finish这个一个像素的Activity
                KeepLiveActivityManager.getInstance(MyService.this).finishKeepLiveActivity();
            }
            
            @Override
            public void onScreenOff() {
                // 锁屏---启动一个像素的Activity
                KeepLiveActivityManager.getInstance(MyService.this).startKeepLiveActivity();
            }
        });
    }
}

权限设置和androidmainfast

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>

<service android:name="com.ricky.keepliveprocess.onepixel.MyService"></service>

监听事件

public class ScreenListener {
    private Context mContext;
    private ScreenBroadcastReceiver mScreenReceiver;
    private ScreenStateListener mScreenStateListener;

    public ScreenListener(Context context) {
        mContext = context;
        mScreenReceiver = new ScreenBroadcastReceiver();
    }

    /**
     * screen状态广播接收者
     */
    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        private String action = null;
        @Override
        public void onReceive(Context context, Intent intent) {
            action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
                mScreenStateListener.onScreenOn();
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
                mScreenStateListener.onScreenOff();
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
                mScreenStateListener.onUserPresent();
            }
        }
    }

    /**
     * 开始监听screen状态
     * 
     * @param listener
     */
    public void begin(ScreenStateListener listener) {
        mScreenStateListener = listener;
        registerListener();
        getScreenState();
    }

    /**
     * 获取screen状态
     */
    @SuppressLint("NewApi")
    private void getScreenState() {
        PowerManager manager = (PowerManager) mContext
                .getSystemService(Context.POWER_SERVICE);
        if (manager.isInteractive()) {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOn();
            }
        } else {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOff();
            }
        }
    }

    /**
     * 停止screen状态监听
     */
    public void unregisterListener() {
        mContext.unregisterReceiver(mScreenReceiver);
    }

    /**
     * 启动screen状态广播接收器
     */
    private void registerListener() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        mContext.registerReceiver(mScreenReceiver, filter);
    }

    public interface ScreenStateListener {// 返回给调用者屏幕状态信息
        public void onScreenOn();

        public void onScreenOff();

        public void onUserPresent();
    }
}
  1. app运营商和手机厂商可能有合作关系---白名单
  2. 双进程守护---可以防止单个进程杀死,同时可以防止第三方的360清理掉。
  • 一个进程被杀死,另外一个进程又被他启动。相互监听启动。
  • 杀进程是一个一个杀的。本质是和杀进程时间赛跑。

使用IPC进程间通信adil

package com.dn.keepliveprocess;
interface RemoteConnection{
    String getProcessName();
}

两个进程

  • 本地进程
public class LocalService extends Service {

    public static final String TAG = "LocalService";
    private MyBinder binder;
    private MyServiceConnection conn;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        if(binder ==null){
            binder = new MyBinder();
        }
        conn = new MyServiceConnection();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
    
            // 把service设置为前台运行,避免手机系统自动杀掉改服务。
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                startForeground(Util.NOTIFICATION_ID, new Notification());
            } else {
                // API 18以上,发送Notification并将其置为前台后,启动InnerService
                startForeground(Util.NOTIFICATION_ID, new Notification());
                startService(new Intent(this, InnerService.class));
            }

        return START_STICKY;
    }
    

    class MyBinder extends RemoteConnection.Stub{

        @Override
        public String getProcessName() throws RemoteException {
            return "LocalService";
        }
        
    }
    
    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "建立连接成功!");  
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "RemoteService服务被干掉了~~~~断开连接!");
            Toast.makeText(LocalService.this, "断开连接", 0).show();
            //启动被干掉的
            LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
        }   
    }
}

第二个进程RemoteService基本上都是一样的只是

@Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "LocalService服务被干掉了~~~~断开连接!");
            //Toast.makeText(RemoteService.this, "断开连接", 0).show();
            //启动被干掉的
            RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));
            RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
        }

这个不同

其中有一个问题是出现通知栏,用户会被发现,使用可以开启一个服务去关闭通知栏

public class InnerService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
   
        startForeground(Util.NOTIFICATION_ID, new Notification());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                stopForeground(true);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.cancel(Util.NOTIFICATION_ID);
                stopSelf();
            }
        }, 100);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

使用调用

startService(new Intent(this, LocalService.class));
startService(new Intent(this, RemoteService.class));

设置权限和mainfast

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<service android:name="com.dn.keepliveprocess.LocalService"/>
<service android:name="com.dn.keepliveprocess.InnerService"/>
<service 
    android:name="com.dn.keepliveprocess.RemoteService"
    android:process=":remoteprocess"/>
  1. JobScheduler

把任务加到系统调度队列中,当到达任务窗口期的时候就会执行,我们可以在这个任务里面启动我们的进程。
这样可以做到将近杀不死的进程。android5.0以上。

调用方法:

<service android:name="com.dn.keepliveprocess.JobHandleService" android:permission="android.permission.BIND_JOB_SERVICE"></service>

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            startService(new Intent(this, JobHandleService.class));
}

JobScheduler类

public class JobHandleService extends JobService{
    private int kJobId = 0;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("INFO", "jobService create"); 
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("INFO", "jobService start");
        scheduleJob(getJobInfo());
        return START_NOT_STICKY;
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    @Override
    public boolean onStartJob(JobParameters params) {
        Log.i("INFO", "job start");
        boolean isLocalServiceWork = isServiceWork(this, "com.dn.keepliveprocess.LocalService");
        boolean isRemoteServiceWork = isServiceWork(this, "com.dn.keepliveprocess.RemoteService");
        if(!isLocalServiceWork||
           !isRemoteServiceWork){
            this.startService(new Intent(this,LocalService.class));
            this.startService(new Intent(this,RemoteService.class));
            Toast.makeText(this, "process start", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i("INFO", "job stop");
        scheduleJob(getJobInfo());
        return true;
    }

    /** Send job to the JobScheduler. */
    public void scheduleJob(JobInfo t) {
        Log.i("INFO", "Scheduling job");
        JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.schedule(t);
    }
    
    public JobInfo getJobInfo(){
        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, JobHandleService.class));
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setRequiresCharging(false);
        builder.setRequiresDeviceIdle(false);
        builder.setPeriodic(10);//间隔时间--周期
        return builder.build();
    }
    
    
    /** 
     * 判断某个服务是否正在运行的方法 
     *  
     * @param mContext 
     * @param serviceName 
     *            是包名+服务的类名(例如:net.loonggg.testbackstage.TestService) 
     * @return true代表正在运行,false代表服务没有正在运行 
     */  
    public boolean isServiceWork(Context mContext, String serviceName) {  
        boolean isWork = false;  
        ActivityManager myAM = (ActivityManager) mContext  
                .getSystemService(Context.ACTIVITY_SERVICE);  
        List<RunningServiceInfo> myList = myAM.getRunningServices(100);  
        if (myList.size() <= 0) {  
            return false;  
        }  
        for (int i = 0; i < myList.size(); i++) {  
            String mName = myList.get(i).service.getClassName().toString();  
            if (mName.equals(serviceName)) {  
                isWork = true;  
                break;  
            }  
        }  
        return isWork;  
    }  
}

  1. 监听QQ,微信,系统应用,友盟,小米推送等等的广播,然后把自己启动了。
  2. 利用账号同步机制唤醒我们的进程。AccountManager
  3. NDK来解决,Native进程来实现双进程守护。

相关文章

  • 第十六周 进程保活

    话题:进程保活 这个问题时常在面试中被问到关键字:Android 进程保活招式大全 参考答案 1.进程保活方案 -...

  • 进程保活与拉活

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

  • 关于进程保活的两三事——新手升级经验卡

    首先,先搁下几个问题,什么是进程保活?为什么要有进程保活?最后才是进程保活要怎么实现??相信大家会迫不及待跳到最后...

  • Android进程保活实践总结

    Android进程保活

  • 深度剖析APP保活案例

    这是作者在去年处理的一个关于进程保活的案例 一. 引言 1.1 保活概述 什么是保活?保活就是在用户主动杀进程,或...

  • 关于 Android 进程保活

    关于 Android 进程保活 Android进程保活手段主要分3种: 1:利用不同的app进程使用广播来进...

  • 进程保活方案学习

    进程保活方案 进程保活主要有两个方案 提高进程优先级,降低死亡几率 在进程被杀死后进行拉活 进程为什么会死亡 从L...

  • 进程保活

    https://www.cnblogs.com/bugly/p/5765334.html

  • 进程保活

    https://mp.weixin.qq.com/s/r3-5Y_e_0fq3_7a8kdGWog

  • 进程保活

    Service service:是一个后台服务,专门用来处理常驻后台的工作的组件。 Service被强的原因: 内...

网友评论

      本文标题:进程保活

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