Android 如何最大限度的保活后台进程

前言:本文将通过两种方式,在手机亮屏和屏锁状态下联合对进程进行保活。

一、手机亮屏时如何提高存活率
在点击home键使app长时间停留在后台时,内存不足时会被杀死。
处理这种情况时运用灰色保活,在service里通过Service.startForeground() 设置为前台服务,提高存活率。

 if (Build.VERSION.SDK_INT < 18) {
            //Android4.3以下 ,隐藏Notification上的图标
            startForeground(GRAY_SERVICE_ID, new Notification());
        } else if (Build.VERSION.SDK_INT > 18 && Build.VERSION.SDK_INT < 25) {
            //Android4.3 - Android7.0,隐藏Notification上的图标
            Intent innerIntent = new Intent(this, GrayInnerService.class);
            startService(innerIntent);
            startForeground(GRAY_SERVICE_ID, new Notification());
        } else {
            //7.0以上暂时没有办法隐藏Notification上的图标 且需要先注册渠道
            String channelId = "my_service";
            String channelName = "前台service";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;//优先级设置为最高
            createNotificationChannel(channelId, channelName, importance);
            Notification notification = new NotificationCompat.Builder(this, "my_service")
                    .build();
            startForeground(GRAY_SERVICE_ID, notification);
        }

GrayInnerService

/**
     * 灰色保活 用于4.3-7.0系统取消通知栏图标
     * */
    public static class GrayInnerService extends Service {

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

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

二、手机锁屏时如何提高存活率

手机在进入锁屏状态一段时间,省电机制会杀死后台进程。
处理这种情况时,我们需注册广播监听锁屏和解锁事件, 锁屏后启动一个1像素的透明Activity, 解锁后销毁这个透明Activity。
注:这个广播要写到要保活的service中。

 //注册锁屏解锁广播
        mOnePixelReceiver = new OnePixelReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(mOnePixelReceiver, filter);

 /**
     * 广播监听,手机锁屏时启动1像素Activity
     * */
    public class OnePixelReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                //屏幕关闭启动1像素Activity
                Intent it = new Intent(context, OnePiexlActivity.class);
                it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(it);

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                //屏幕打开 发送通知验证没死 
                sendNotification();
            }
        }
    }

OnePiexlActivity.class

*
* 透明 Activity 用户不察觉
**/
public class OnePiexlActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置1像素
        Window window = getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 1;
        params.width = 1;
        window.setAttributes(params);
        //检查屏幕状态
        checkScreen();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkScreen();
    }

    /**
     * 检查屏幕状态 isScreenOn为true 屏幕“亮”结束该Activity
     */
    private void checkScreen() {
        PowerManager pm = (PowerManager) OnePiexlActivity.this.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isScreenOn();
        if (isScreenOn) {
            finish();
        }
    }
}

总结:以上就是在手机锁屏和亮屏时对进程进行保活的两种方法
源码地址:https://download.csdn.net/download/u014085912/11912451

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容