Android 2.0 检查电池电量/充电的最佳方法?
好吧,首先我要说的是,我一直在四处寻找这个问题的答案。我试图找出最佳做法以及最有利于电池寿命的做法。这是我的情况:
我希望我的应用程序在设备达到某个用户定义的电池电量时暂停其活动,而不必使用 Intent.ACTION_BATTERY_LOW 和 Intent.ACTION_BATTERY_OKAY。我在很多程序中都看到过这个选项,并且想在我的应用程序中模拟它。
我知道 Intent.ACTION_BATTERY_CHANGED 必须是注册事件。您不能仅仅在 BroadcastReceiver 的清单中声明它来让该接收者获取 Intent。我知道如何编码如何获取电池电量/电量,并查看设备是否通过 USB 或 AC 充电
我当前的思维过程给我带来了 2 个选择,但我无法找出哪个是最佳选择,或者是否还有另一个我不知道的选择?也许有人可以帮助我了解各种选项的利弊来帮助我?
选项 1: 使用 AlarmManager 创建重复警报,基本上每隔一段时间轮询设备以检查电池电量。这样,当我的警报响起时,它会向我的清单注册接收器发送自定义意图来检查电池电量。
PRO 没有运行后台服务,可能会被杀死或耗尽电池寿命。
CON 无法实时检测设备从充电源插入/拔出的情况,并且必须依赖下一次发出的警报来检测变化。
选项2:创建一个为Intent.ACTION_BATTERY_CHANGED注册接收器的服务,这样它不仅可以在系统广播时获取电池电量,而且可以实时检测充电变化。
PRO实时检测电池寿命变化和充电方式变化。
CON 是一个持续运行的服务,可能会被杀死
CON 会消耗处理时间和电池寿命,以保持检测继续进行
电池电量:
在广播接收器响应 Intent.ACTION_BATTERY_CHANGED 之前获取电池电量
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra("level", -1);
double scale = batteryIntent.getIntExtra("scale", -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
level = rawlevel / scale;
插入/充电:
public static boolean isConnected(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
}
提前致谢
-H
Okay let me start off by saying I have been looking around a lot for an answer to this question. I am trying to figure out what is best practice and what best for battery life. Here is my situation:
I would like my application to suspend its activities when the device reaches a certain user defined battery level and not have to use the Intent.ACTION_BATTERY_LOW and Intent.ACTION_BATTERY_OKAY. I have seen this option in a lot of programs and would like to emulate it with my application.
I understand that the Intent.ACTION_BATTERY_CHANGED must be a registered event. You can not just declare it in the manifest for a BroadcastReceiver to get that receiver to get the Intent. I know how to code up how to get the battery level / scale and see if the device is being charged via USB or AC
My current thought process has brought me to 2 choices and I can't find out which is the best option or if there is another option that I am not aware of?? Maybe someone could help me with the pros and cons of the options to help me out??
Option 1: Create a repeating alarm using the AlarmManager to basically poll the device every so often to check the battery level. So that when my alarm fires it would send a custom Intent for my manifest registered receiver to check the battery level.
PRO no background service running that could get killed or eat up battery life.
CONs not having realtime detection of plugging/unplugging the device from a charging source and having to rely on the next fired alarm to detect the change.
Option 2: Create a service that registers a receiver for Intent.ACTION_BATTERY_CHANGED so it will not only get the battery level when it is broadcast by the system, but will detect the changes in charging realtime.
PRO realtime detection of battery life change and changes to charging method.
CON a constant service running that could get killed
CON would eat up processing time and battery life in order to keep the detection going
Battery Level:
Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra("level", -1);
double scale = batteryIntent.getIntExtra("scale", -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
level = rawlevel / scale;
Plugged In/Charging:
android USB connection charging signals
public static boolean isConnected(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
}
Thanks in advance
-H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我想出的解决方案,
我创建了一个警报来触发X秒数,我想我将其设置为90,以发送自定义意图。然后我发现 android.intent.action.ACTION_POWER_CONNECTED 和 android.intent.action.ACTION_POWER_DISCONNECTED 允许我检查设备是否已插入。使用上述功能的组合,我能够检查我的电池电量以及设备是否正在充电,而无需使用注册的接收器创建持续的服务。我希望这至少对那里的人有帮助。
Here is the solution I came up with
I created an Alarm to fire off X number of seconds, I think I set it to 90, to send a custom Intent. I then found that android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED allowed me to check to see if the device was plugged in or not. Using a combination of the above mention functions I was able to check my battery level and if the device was charging without having to create a constant service with a registered receiver. I hope this helps at least someone out there.