跑步app保活_android APP保活机制

本文探讨了在Android系统中如何使跑步App在暗屏或休眠状态下保持后台执行。Google为优化用户体验和电池寿命,对后台进程进行了限制。文章介绍了使用Notification配合startForeground()方法以及设置Wakelock来维持Service常驻,但持续使用Wakelock可能导致电量消耗过高而被系统回收。作者提出使用定时器唤醒CPU的策略,通过WakefulBroadcastReceiver和WakecpuIntentService实现后台任务执行,确保服务运行的同时降低能耗。
摘要由CSDN通过智能技术生成

公众号:QXF069

摘要:

需要APP暗屏情况下进行后台执行一些任务,但是google现在为了优化手机的体验,以及流畅性,做了很多的限制,电量优化,休眠模式,以及进入深度睡眠状态,进入深入休眠状态系统会根据黑白名单的应用进行管理,杀掉非白名单的后台进程以及网络请求;Android系统的优化是守护后台进程造成了很大困扰,线程之间守护已经是不在可靠,使用wakelock耗电过大也会是系统杀掉应用进程。下面就对我们的经历说说守护进程的。

1,从官网上可以看出google为了系统更加流畅以及优化内存,Google做了很大的处理,在手机暗屏或者睡眠状态就停止后台运行;若要保持service的常驻,需要做一些前端的活动,Notification重要属性:notification.flags

=

Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;然后startForeground(setClass().hashCode(),

notification);使得服务能挂在通知栏。

2,通过wakelock占用CPU,若一直占用CPU的话,当然这是比较耗电的,要是耗电太高的话系统会直接回收,杀死程序进程。

我是使用定时器唤醒cpu 后台服务将接收到定时任务执行未完成的任务:下面是我实现的代码

在mainfest中注册

android:name="com.txtws.wakeloackdemo.service.WakecpuIntentService" >

关键代码定时任务:

public void startAlarm(Context context){

Intent intent = new Intent(context, WakeCPUAlarmReceiver.class);

alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr = (AlarmManager) context

.getSystemService(Context.ALARM_SERVICE);// alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,// 1000, alarmIntent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

alarmMgr.setExactAndAllowWhileIdle(

AlarmManager.ELAPSED_REALTIME_WAKEUP,

SystemClock.elapsedRealtime() + chekcTime, alarmIntent);

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,

SystemClock.elapsedRealtime() + chekcTime, alarmIntent);

} else {

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,

SystemClock.elapsedRealtime() + chekcTime, alarmIntent);

}

FileLogUtil.e(TAG , "startAlarm 设置定时任务");

}

接收到定时任务:

public class WakeCPUAlarmReceiver extends WakefulBroadcastReceiver { private AlarmManager alarmMgr; private PendingIntent alarmIntent; private final long chekcTime = 30* 1000;

public static boolean isCheckFlag = false; private static String TAG = WakeCPUAlarmReceiver.class.getSimpleName(); @Override

public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub

Intent service = new Intent(context, WakecpuIntentService.class); FileLogUtil.e(TAG , "onReceive 接收定时任务");

startWakefulService(context, service);

}

在service中执行需要执行的任务:

public class WakecpuIntentService extends IntentService { private static String TAG = WakecpuIntentService.class.getSimpleName() + " :"; private NotificationManager mNotificationManager; private PowerManager.WakeLock wl; NotificationCompat.Builder builder;

public WakecpuIntentService() { super("WakecpuIntentService"); // TODO Auto-generated constructor stub

} @Override

protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub

Bundle extras = intent.getExtras(); FileLogUtil.e(TAG, "Service接收到WakeLock定时任务。"); PollService.startService(getApplicationContext()); FileLogUtil.e(TAG, "Service接收到WakeLock定时任务在定时任务中启动轮询Service。"); // Do the work that requires your app to keep the CPU running.

// Release the wake lock provided by the WakefulBroadcastReceiver.

//checkScreenOff();

WakeCPUAlarmReceiver.completeWakefulIntent(intent);//释放wake

WakeCPUAlarmReceiver.setCheckFlag(false); WakeCPUAlarmReceiver mAlarmReceiver = new WakeCPUAlarmReceiver();

mAlarmReceiver.startAlarm(WakecpuIntentService.this); FileLogUtil.e(TAG, "Service释放wakelock后设置定时任务。");

}

}

service在后台也能够打印log 保持一直执行,最重要的是不耗电也不会卡机的问题。欢迎大家一起讨论学习。

关注公众号:QXF069

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值