1. 程式人生 > >Android 程序保活--1畫素保活

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殺死,使用者上滑應用殺死,手機安裝手機衛士等都會導致應用給殺死。所以,目前保活只能提高優先順序,絕對不可能存在不死應用。

如果覺得我的文章對您有用,請點贊。您的支援將鼓勵我繼續碼蛋!