android wakelock service,Android: Foreground service vs. wakeLock

在Android中,为了长时间连续监测加速度计,有使用部分唤醒锁和前台服务两种方法。持有唤醒锁会大量消耗电池,而前台服务虽然更不易被杀死,但会显示通知图标。结合两者可以确保监测,但可能对用户体验造成影响。建议根据应用需求,如仅在活动处于前台时监测加速度计,以平衡电池寿命和用户体验。
摘要由CSDN通过智能技术生成

问题

I am relatively new to Android, so what I am asking may seem obvious (although I have read all the similarly titled questions, and have searched extensively). I need to monitor the accelerometer continuously for long periods. Two approaches have been suggested:

1) acquire a partial wake lock that is held the entire time the acceleromtere is being monitored; and

2) monitor the accelerometer in a foreground service.

The first approach appears to use a lot of battery life. The second approach should result in a service that is only killed rarely, but I'm not sure what "rarely" means. Which approach should be used, and are there alternatives that I should consider?

回答1:

Holding a WakeLock and a foreground Service are not really related and shouldn't be compared are to which direction is best.

Android OS is built to swap out processes based on a variety of factors. This means your process might get killed at any point by Android and it provides a framework to help you, the developer, to ensure your app can save and restore its state when this happens.

A WakeLock simply prevents the CPU from sleeping which helps save battery when the phone is not in use.

Now, a combination of both would help you achieve what you want but at great user cost. I wouldn't want an app in my phone to keep the CPU constantly running or a notification icon to show up constantly in the notification bar (that's what a foreground service does).

Keep in mind, starting a service in foreground mode does not guarantee your app will not get killed. It might still happen albeit rarely.

What is it you are trying to achieve here? Why keep monitoring the devices accelerometer? Perhaps you should only monitor it only when an Activity of your app is in the foreground instead.

回答2:

I had exactly the same need and problem. I believe the solution is to use both a partial wake lock and a foreground service. Android will try not to kill a background service that holds a wake lock but is free to kill it when it needs the resources and possibly restart it later. That's fine for a lot of purposes but at least in my case that is not good enough. Putting a service into the foreground state is the way to tell Android that that killing it is unacceptable. Yes, it might still happen in extreme situations but that would now be a violation of the API contract whereas with a background service Android is free to kill it. You should therefore probably code as if that that will never happen but just know that this is a possible but probably rare error.

来源:https://stackoverflow.com/questions/9104622/android-foreground-service-vs-wakelock

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值