Android 进程保活--1像素保活

在Android开发中,必定有一些应用是需要常驻后台运行的,比如长期对某个事物的监听或者长期扫描等等。如果Android手机锁屏了,就有一定几率会给手机厂商的OS系统给杀死。所以,为了在手机锁屏之后避免应用给杀死,我们可以选择提高进程的优先级,所以使用1像素Activity进行保活。

保活简单实现上码

/**
 * 1像素A
 */
public class ProtectActivity extends BeBoyBaseActivity {

    public static WeakReference<ProtectActivity> weakReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_protect);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        weakReference = new WeakReference<>(this);
        KLog.e("初始化");
        Window window = getWindow();
        //放在左上角
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        //起始坐标
        params.x = 0;
        params.y = 0;
        //宽高设计为1个像素
        params.height = 1;
        params.width = 1;
        window.setAttributes(params);
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        finishSelf();
        return super.dispatchTouchEvent(motionEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        finishSelf();
        return super.onTouchEvent(motionEvent);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isScreenOn()) {
            finishSelf();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        KLog.e("销毁保活页面");
        if (weakReference != null && weakReference.get() == this) {
            weakReference = null;
        }
    }

    /**
     * 关闭自己
     */
    public void finishSelf() {
        if (!isFinishing()) {
            finish();
        }
    }

    /**
     * 判断主屏幕是否点亮
     *
     * @return
     */
    private boolean isScreenOn() {
        PowerManager powerManager = (PowerManager) getApplicationContext()
                .getSystemService(Context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            return powerManager.isInteractive();
        } else {
            return powerManager.isScreenOn();
        }
    }

}

广播走起

public class ScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            KLog.e("锁屏");
            Intent intentNew = new Intent(context, ProtectActivity.class);
            intentNew.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(intentNew);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            KLog.e("解锁");
            ProtectActivity protectActivity = ProtectActivity.weakReference != null ? ProtectActivity.weakReference.get() : null;
            protectActivity.finish();
//            Intent intentNew = new Intent(context, WelcomeActivity.class);
//            intentNew.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            context.startActivity(intentNew);
        }
    }
}

Appcation动态注册广播

  private void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(new ScreenReceiver(), intentFilter);
    }

完成,这就是我们所说的1像素保活。

原理:

提高进程的优先级,使APP避免过快给系统杀死。

局限性:

目前手机内置OS也会将APP杀死,用户上滑应用杀死,手机安装手机卫士等都会导致应用给杀死。所以,目前保活只能提高优先级,绝对不可能存在不死应用。

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续码蛋!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值