1像素保活原理是,监听手机屏幕状态,如果手机黑屏,则启动一个1像素的Activity,减少该应用被回收的几率;在屏幕亮的时候,就关闭该Activity。1像素保活对某些手机不管用,亲测两款手机:红米5Note(Android 9)和华为G9青春版(Android 6.0)。
测试方法:看是否调用1像素Activity。在打开应用,然后息屏情况下,两款手机都会调用1像素Activity;打开应用,然后点home键,再息屏,华为G9青春版(Android 6.0)会调用1像素Activity,但是红米5Note(Android 9)则不会调用。
首先建立一个OnePixelManager,该类是一个管理类。代码如下
package com.yds.jianshu.onepixel;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.lang.ref.WeakReference;
public class OnePixelManager {
private static final String TAG = "[OnePixelManager]";
private WeakReference<Activity> mActivity;
private OnePixelReceiver onePixelReceiver;
/**
* 一像素广播接收者注册方法。该方法中初始化OnePixelReceiver,并添加了过滤条件
* 屏幕息屏和亮屏。然后注册该广播接收者
* @param context
*/
public void registerOnePixelReceiver(Context context){
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
onePixelReceiver = new OnePixelReceiver();
context.registerReceiver(onePixelReceiver,filter);
}
/**
* 对广播接收者进行解注册
* @param context
*/
public void unregisterOnePixelReceiver(Context context){
if (null != onePixelReceiver){
context.unregisterReceiver(onePixelReceiver);
}
}
/**
* 开启一像素Activity
* @param context
*/
public void startOnePixelActivity(Context context){
Intent intent = new Intent();
intent.setClass(context,OnePixelActivity.class);
context.startActivity(intent);
}
/**
* 关闭一像素Activity
*/
public void finishOnePixelActivity(){
if(null!=mActivity){
Activity activity = mActivity.get();
if(null!=activity){
activity.finish();
}
mActivity = null;
}
}
/**
* 使用弱引用获取一像素的上下文
* @param activity
*/
public void setKeepAliveReference(OnePixelActivity activity){
mActivity = new WeakReference<Activity>(activity);
}
}
写一个一像素的广播接收者,接收息屏和亮屏时的广播
package com.yds.jianshu.onepixel;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnePixelReceiver extends BroadcastReceiver {
private static final String TAG = "[OnePixelReceiver]";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
OnePixelManager manager = new OnePixelManager();
if (Intent.ACTION_SCREEN_ON.equals(action)){//如果亮屏,则关闭1像素Activity
manager.finishOnePixelActivity();
}else if(Intent.ACTION_SCREEN_OFF.equals(action)){//如果息屏,则开启1像素Activity
manager.startOnePixelActivity(context);
}
}
}
创建一个1像素的Activity,并将引用传给OnePixelManager
package com.yds.jianshu.onepixel;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
public class OnePixelActivity extends Activity{
private static final String TAG = "[OnePixelActivity]";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.setGravity(Gravity.LEFT|Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.height = 1;
params.width = 1;
window.setAttributes(params);
OnePixelManager manager = new OnePixelManager();
manager.setKeepAliveReference(this);//将引用传给OnePixelManager
Log.e(TAG,"onCreate");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG,"onDestroy");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG,"onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG,"onPause");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG,"onResume");
}
}
package com.yds.jianshu.mobile;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.R;
import com.yds.jianshu.onepixel.OnePixelManager;
public class MainActivity extends AppCompatActivity{
private static final String TAG = "[MainActivity]";
OnePixelManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = new OnePixelManager();
manager.registerOnePixelReceiver(this);//注册广播接收者
}
@Override
protected void onDestroy() {
super.onDestroy();
manager.unregisterOnePixelReceiver(this);//Activity退出时解注册
}
}
在OnePixelActivity注册的地方要加上以下三句
android:excludeFromRecents="true"//#1
android:launchMode="singleTask"//#2
android:taskAffinity="com.yds.task"//#3
代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.yds.jianshu.mobile.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<activity android:name="com.yds.jianshu.onepixel.OnePixelActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:taskAffinity="com.yds.task"
android:theme="@style/OnePixelActivity">
activity>
application>
manifest>
将OnePixelActivity背景设置为透明
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- "colorPrimary"
>@color/colorPrimary
- "colorPrimaryDark">@color/colorPrimaryDark
- "colorAccent">@color/colorAccent
style>
<style name="OnePixelActivity" parent="android:Theme.Holo.Light.NoActionBar">//无标题
- "android:windowIsTranslucent"
>true//透明
style>
resources>