Android(安卓)进程保活--1像素保活
16lz
2021-01-25
在Android开发中,必定有一些应用是需要常驻后台运行的,比如长期对某个事物的监听或者长期扫描等等。如果Android手机锁屏了,就有一定几率会给手机厂商的OS系统给杀死。所以,为了在手机锁屏之后避免应用给杀死,我们可以选择提高进程的优先级,所以使用1像素Activity进行保活。
保活简单实现上码
/** * 1像素A */public class ProtectActivity extends BeBoyBaseActivity { public static WeakReference 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杀死,用户上滑应用杀死,手机安装手机卫士等都会导致应用给杀死。所以,目前保活只能提高优先级,绝对不可能存在不死应用。
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续码蛋!
更多相关文章
- Android(安卓)studio无法连接识别检测各种模拟器和手机的问题
- 三星首款Android手机上市
- 50款Android手机必备应用大全
- Pantech Android(安卓)手机使用 DDR2 内存
- Android游戏的盈利模式探讨
- Unity NGUI UI 在iOS端的锯齿、模糊、颗粒感问题
- 移动平台前端开发总结(针对iphone,Android等手机)
- 经验总结-Android手机屏幕适配问题
- 恩布900手机客服,安卓版本号Entboost Android(安卓)1.0 正式公布