Android之一像素保活

在开发中,有一些应用是需要常驻后台运行:长期对某个事物的监听或者长期扫描等。若手机锁屏,有一定几率被系统给杀死。

在手机锁屏之后避免应用给杀死,通过提高进程的优先级,使用1像素Activity进行保活。

原理:

当屏幕熄灭时,锁屏状态,调起一个一像素的Activity,提高进程的优先级。

一像素的Activity:

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import java.lang.ref.WeakReference;

/**
 * @author Zachary
 * 一像素保活
 */
public class ProtectActivity extends Activity {

    public static WeakReference weakReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    protected void initView() {
        weakReference = new WeakReference<>(this);
        Log.e("save", "初始化");
        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);
    }

    protected void initData() {
    }

    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();
        Log.e("save", "销毁保活页面");
        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();
        }
    }

}

在AndroidManifest.xml配置:

ProtectActivity配置为透明。

锁屏广播:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.zachary.util.Activity.ProtectActivity;

public class ScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.e("save", "锁屏");
            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)) {
            Log.e("save", "解锁");
            ProtectActivity protectActivity = ProtectActivity.weakReference != null ? ProtectActivity.weakReference.get() : null;
            protectActivity.finish();
        }
    }
}

使用方法:

public class MainActivity extends SuperActivity{

    private ScreenReceiver screenReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // 保活,监控关屏幕
        registerReceiver();
    }

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


    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(screenReceiver);
    }
}

 

你可能感兴趣的:(Android之从头自学)