- 保活方式
- 原理: 创建一个像素点的activity,通过广播监听关屏时打开 , 开屏时移出 这activity 因此来达到降低 oom_obj的值来进行保活
像素点KeepActivity
package com.example.amn.amnimageload.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
/**
* Created by Amn on 2019/4/20.
*/
public class KeepActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window= getWindow();
//设置activity 左上角
window.setGravity(Gravity.START | Gravity.TOP);
// 创建宽高都 是 1 像素
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width=1;
attributes.height=1;
attributes.x=0;
attributes.y=0;
window.setAttributes(attributes);
//设置弱引用
KeepManager.getInstance().setKeep(this);
}
}
广播监听关屏还是息屏 KeepReceive
package com.example.amn.amnimageload.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
/**
* Created by Amn on 2019/4/20.
*/
public class KeepReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
//息屏
if(TextUtils.equals(action,intent.ACTION_SCREEN_OFF)){
KeepManager.getInstance().startKeep(context);
}else if(TextUtils.equals(action,intent.ACTION_SCREEN_OFF)){
//开屏
KeepManager.getInstance().finishKeep();
}
}
}
管理广播的注册 反注册,开启像素activity 的管理类 KeepManager
package com.example.amn.amnimageload.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.lang.annotation.Retention;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
/**
* Created by Amn on 2019/4/20.
*/
public class KeepManager {
private static KeepManager instance=new KeepManager();
public static KeepManager getInstance(){
return instance;
}
private KeepManager(){
}
private KeepReceive keepReceive;
private WeakReference<Activity> mActivity;
/**
* 注册关屏 开屏
*/
public void registerKeep(Context context){
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
keepReceive=new KeepReceive();
context.registerReceiver(keepReceive,intentFilter);
}
/**
* 注销广播
*/
public void unregisterKeep(Context context){
if(null != keepReceive){
context.unregisterReceiver(keepReceive);
}
}
/**
* 开启1像素的activity
*
*/
public void startKeep(Context context){
Intent intent=new Intent(context,KeepActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/**
* 关闭activity
*/
public void finishKeep(){
if(null != mActivity){
Activity activity= mActivity.get();
if(null != activity){
activity.finish();
}
mActivity =null;
}
}
//绑定弱引用
public void setKeep(KeepActivity activity){
mActivity=new WeakReference<Activity>(activity);
}
}
Manifest配置
<activity android:name=".activity.KeepActivity"
android:excludeFromRecents="true"
android:taskAffinity="com.example.amn.amnimageload"
android:theme="@style/KeepTheme"
></activity>
在activity 使用
package com.example.amn.amnimageload;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.amn.amnimageload.activity.KeepManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过 1 像素acticitity 提升优先级
KeepManager.getInstance().registerKeep(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
KeepManager.getInstance().unregisterKeep(this);
}
}