Android - 保活(2)一像素保活

老婆保佑,代码无BUG

目录

  • 一:原理
  • 二:实现
      1. 一像素的Act
      1. 广播
      1. 清单文件注册
      1. 透明主题
      1. 注册广播

一:原理

当屏幕熄灭的时候呢,锁屏状态,调起一个一像素的Activity,为的还是能够提高进程的优先级,在上一篇文章中,有详细的进程优先级的情况

二:实现

1. 一像素的Act

package com.cpsc.sanya.act;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

/**
 * 描述:
 * 

*

* 一像素的ACT * * @author allens * @date 2018/1/24 */ public class OnePxActivity extends Activity { private static final String TAG = "OnePx"; private BroadcastReceiver endReceiver; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate--->启动1像素保活"); // 获得activity的Window对象,设置其属性 Window mWindow = getWindow(); mWindow.setGravity(Gravity.LEFT | Gravity.TOP); WindowManager.LayoutParams attrParams = mWindow.getAttributes(); attrParams.x = 0; attrParams.y = 0; attrParams.height = 1; attrParams.width = 1; mWindow.setAttributes(attrParams); //结束该页面的广播 endReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive--->finish"); finish(); } }; registerReceiver(endReceiver, new IntentFilter("finish")); checkScreen(); } @Override protected void onResume() { super.onResume(); } /** * 检查屏幕状态 isScreenOn为true 屏幕“亮”结束该Activity */ private void checkScreen() { PowerManager pm = (PowerManager) OnePxActivity.this.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); if (isScreenOn) { finish(); } } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(endReceiver); } }

2. 广播

package com.cpsc.sanya.receiver;

import android.content.BroadcastReceiver;

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

import com.cpsc.sanya.act.OnePxActivity;

/**
 * 描述:
 * 

*

* 广播 * * @author allens * @date 2018/1/24 */ public class OnePixelReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //屏幕关闭启动1像素Activity if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { Log.e("OnePx", "屏幕关闭启动1像素Activity"); Intent it = new Intent(context, OnePxActivity.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); //屏幕打开 结束1像素 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.e("OnePx", "屏幕打开 结束1像素"); context.sendBroadcast(new Intent("finish")); } } }

3. 清单文件注册

  
        

4. 透明主题

  


5. 注册广播

 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(Intent.ACTION_SCREEN_ON);
 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
 registerReceiver(new OnePixelReceiver(), intentFilter);

你可能感兴趣的:(Android - 保活(2)一像素保活)