收集整理android 进程保活方法
1像素Activity
注册监听屏幕开启和屏幕关闭时的广播,当屏幕关闭时,开启1像素的
Activity
, 当屏幕开启时,关闭1像素的Activity
.
查看进程等级
可以在
studio
终端进行命令行,pid
进程id
1
2
3adb shell
su
cat proc/{pid}/oom_adjOnePxActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32public class OnePxActivity extends AppCompatActivity {
public static void launch(Context context){
Intent intent = new Intent(context,OnePxActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("qq","OnPxActivity onCreate.........");
Window window = getWindow();
window.setGravity(Gravity.START | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.width = 1;
params.height = 1;
window.setAttributes(params);
KeepAliveManager.getInstance().setKeepAliveManager(this);
}
protected void onDestroy() {
super.onDestroy();
Log.i("qq","OnPxActivity onDestroy.........");
}
}广播 : 监听开启/关闭的广播
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class KeepAliveReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
//屏幕点亮
Log.i("qq","收到屏幕开启广播");
KeepAliveManager.getInstance().finishOnePxActivity(context);
}else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
//屏幕熄灭, 启动activity
Log.i("qq","收到屏幕关闭广播");
KeepAliveManager.getInstance().startOnePxActivity(context);
}
}
}辅助类:
KeepAliveManager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38public class KeepAliveManager {
private KeepAliveManager(){}
private static KeepAliveManager mInstance = new KeepAliveManager();
private WeakReference<OnePxActivity> mReference;
public void setKeepAliveManager(OnePxActivity activity){
this.mReference = new WeakReference<>(activity);
}
public static KeepAliveManager getInstance(){
return mInstance;
}
public void startOnePxActivity(Context context){
OnePxActivity.launch(context);
}
public void finishOnePxActivity(Context context){
if(null != mReference && mReference.get() != null){
mReference.get().finish();
}
}
private KeepAliveReceiver mReceiver;
public void registerReceiver(Context context){
this.mReceiver = new KeepAliveReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
context.registerReceiver(mReceiver,filter);
}
public void unregisterReceiver(Context context){
if(null != mReceiver){
context.unregisterReceiver(mReceiver);
}
}
}MainActivity
使用:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一种方式
KeepAliveManager.getInstance().registerReceiver(this);
}
protected void onDestroy() {
super.onDestroy();
KeepAliveManager.getInstance().unregisterReceiver(this);
}
}
亲身实践了,发现确实oom_adj
变小,被杀死的概率比较低了,当屏幕变暗的时候。
前台服务
ForegroundService.java
:
1 | public class ForgroundService extends Service { |
双进程守护
后续补上…