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

2018-02-27 11:19:41来源:https://www.jianshu.com/p/a1bddadfb556作者:Allens_Jiang人点击

分享





老婆保佑,代码无BUG
目录
一:原理
二:实现

一像素的Act

广播

清单文件注册

透明主题

注册广播


一:原理

当屏幕熄灭的时候呢,锁屏状态,调起一个一像素的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;
/**
* 描述:
* <p>
* <p>
* 一像素的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;
/**
* 描述:
* <p>
* <p>
* 广播
*
* @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. 清单文件注册
  <!-- 一像素 -->
<activity
android:name="com.cpsc.sanya.act.OnePxActivity"
android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:theme="@style/OnePxActStyle" />

4. 透明主题
  <style name="OnePxActStyle">
<!-- 窗体背景颜色为透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 窗体没有边框 -->
<item name="android:windowFrame">@null</item>
<!-- 窗体不包含标题栏 -->
<item name="android:windowNoTitle">true</item>
<!-- 窗体悬浮 -->
<item name="android:windowIsFloating">true</item>
<!-- 自定义TitleBar时去掉多余的阴影-->
<item name="android:windowContentOverlay">@null</item>
<!-- 不允许窗体背景变暗-->
<item name="android:backgroundDimEnabled">false</item>
<!-- 窗体切换无动画-->
<item name="android:windowAnimationStyle">@null</item>
<!-- 禁用窗口的预览动画-->
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoDisplay">false</item>
</style>


5. 注册广播
 IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new OnePixelReceiver(), intentFilter);







最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台