app安装在vitrual xposed里面很容易被杀,导致掉线等一系列问题,即使开启了各种权限,关闭电源优化,用了wakelock锁还是没有效果。
推荐方法:使用service+AlarmManager进行保活,定时唤醒service,由于是系统时钟,即使进程休眠了也能唤醒,有效地解决了app被杀问题。
代码如下:
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
if(alarmManager!=null) {
Intent intent=new Intent(mContext,Myservice.class);
PendingIntent pendingIntent=PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, 5000, pendingIntent);
}