前言:本文将通过两种方式,在手机亮屏和屏锁状态下联合对进程进行保活。
一、手机亮屏时如何提高存活率
在点击home键使app长时间停留在后台时,内存不足时会被杀死。
处理这种情况时运用灰色保活,在service里通过Service.startForeground() 设置为前台服务,提高存活率。
if (Build.VERSION.SDK_INT < 18) {
//Android4.3以下 ,隐藏Notification上的图标
startForeground(GRAY_SERVICE_ID, new Notification());
} else if (Build.VERSION.SDK_INT > 18 && Build.VERSION.SDK_INT < 25) {
//Android4.3 - Android7.0,隐藏Notification上的图标
Intent innerIntent = new Intent(this, GrayInnerService.class);
startService(innerIntent);
startForeground(GRAY_SERVICE_ID, new Notification());
} else {
//7.0以上暂时没有办法隐藏Notification上的图标 且需要先注册渠道
String channelId = "my_service";
String channelName = "前台service";
int importance = NotificationManager.IMPORTANCE_DEFAULT;//优先级设置为最高
createNotificationChannel(channelId, channelName, importance);
Notification notification = new NotificationCompat.Builder(this, "my_service")
.build();
startForeground(GRAY_SERVICE_ID, notification);
}
GrayInnerService
/**
* 灰色保活 用于4.3-7.0系统取消通知栏图标
* */
public static class GrayInnerService extends Service {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(GRAY_SERVICE_ID, new Notification());
stopForeground(true);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
二、手机锁屏时如何提高存活率
手机在进入锁屏状态一段时间,省电机制会杀死后台进程。
处理这种情况时,我们需注册广播监听锁屏和解锁事件, 锁屏后启动一个1像素的透明Activity, 解锁后销毁这个透明Activity。
注:这个广播要写到要保活的service中。
//注册锁屏解锁广播
mOnePixelReceiver = new OnePixelReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mOnePixelReceiver, filter);
/**
* 广播监听,手机锁屏时启动1像素Activity
* */
public class OnePixelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//屏幕关闭启动1像素Activity
Intent it = new Intent(context, OnePiexlActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//屏幕打开 发送通知验证没死
sendNotification();
}
}
}
OnePiexlActivity.class
*
* 透明 Activity 用户不察觉
**/
public class OnePiexlActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置1像素
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);
//检查屏幕状态
checkScreen();
}
@Override
protected void onResume() {
super.onResume();
checkScreen();
}
/**
* 检查屏幕状态 isScreenOn为true 屏幕“亮”结束该Activity
*/
private void checkScreen() {
PowerManager pm = (PowerManager) OnePiexlActivity.this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if (isScreenOn) {
finish();
}
}
}
总结:以上就是在手机锁屏和亮屏时对进程进行保活的两种方法
源码地址:https://download.csdn.net/download/u014085912/11912451