提示减少电池消耗的Andr​​oid服务? [英] Tips for reducing battery drain for android services?

查看:119
本文介绍了提示减少电池消耗的Andr​​oid服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了一个应用程序的,那么我很失望的服务多少电池消耗。我去拨打电话昨天找到我的电池是在9%;我检查了android系统的静态电池,发现我的应用程序负责的耗电的60%

I wrote an app recently and, well I'm quite disappointed about how much battery the service consumes. I go to make a call yesterday to find my battery is at 9%; I check the android system statics for the battery and find that my app is responsible for 60% of the battery drainage

我的问题是,有什么可以一个呢,以减少对运行的应用程序的电池使用情况,然后睡眠取值60秒?该服务是从SQLite数据库读取;我可以缓存的数据,但会真的考虑那么多电池使用?什么是降低服务的耗电一些标准的方法呢?

My question is, what can one do to reduce the battery usage on an app that runs and then sleeps for 60 seconds? The service is reading from a SQLite database; I could cache the data, but would that really account for that much battery usage? What are some standard ways to reduce battery drainage in a service?

推荐答案

您应该考虑使用的 AlarmManager 来安排你的应用程序或服务被调用时必要的。这个拥有超过当前的唤醒锁方法的一大优势,因为即使是局部唤醒锁将保持CPU的运行。一个AlarmManager报警甚至可以从CPU睡眠状态唤醒的电话。

You should look into using AlarmManager to schedule your app or service to be called when necessary. This has a big advantage over your current wake lock method, because even a partial wake lock will keep the CPU running. An AlarmManager alarm can wake the phone even from CPU sleep.

基本上,摆脱现有的唤醒锁,并安排一个AlarmManager报警,可重复一次一分钟,如果这是你需要到什么,如果有必要唤醒设备,以及向您发送消息。

Basically, get rid of your existing wake lock and schedule an AlarmManager alarm—which can repeat once a minute, if that's what you need—to wake up the device, if necessary, and send you a message.

该AlarmManager本身将拿出一个唤醒锁,同时调用一个的onReceive()方法来通知您报警,并放弃它时的onReceive()完成,让手机回去进入深睡眠,如果它想。

The AlarmManager itself will take out a wake lock while calling an onReceive() method to notify you of the alarm, and relinquish it when onReceive() finishes, letting the phone go back into deep sleep if it wants to.

请注意,这意味着,如果你想要做的扩展工作,例如。射击的东西掉在后台线程 - 你可能想利用自己在醒来的onReceive(锁定),并放弃它时,你的工作就完成了,否则手机可能会去当你在工作中间是睡觉

Note that this means that if you want to do extended work—e.g. firing something off on a background thread—you'll probably want to take your own wake lock out in onReceive() and relinquish it when your work is done, otherwise the phone may go to sleep while you're in the middle of the work.

这是所有pretty在的AlarmManager文档,但我见过的最好的解释是在马克·墨菲的的忙codeR指南Android开发的;他还提供库的Github上正是这种模式。绝对值得一看。

This is all pretty well-explained in the AlarmManager docs, but the best explanation I've seen is in Mark Murphy's The Busy Coder's Guide to Android Development; he also provides a library for exactly this pattern on Github. Definitely worth a look.

这篇关于提示减少电池消耗的Andr​​oid服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆