java 如何检测电池电量低的时间:Android?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13228849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect when the Battery's low : Android?
提问by Adarsh H S
I want to close my app when the battery level of the device gets low. I have added following codes in manifest.
当设备的电池电量变低时,我想关闭我的应用程序。我在清单中添加了以下代码。
<receiver android:name=".BatteryLevelReceiver"
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW" />
<action android:name="android.intent.action.ACTION_BATTERY_OKAY" />
</intent-filter>
</receiver>
And following code in receiver
并在接收器中遵循以下代码
public class BatteryLevelReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "BAttery's dying!!", Toast.LENGTH_LONG).show();
Log.e("", "BATTERY LOW!!");
}
}
I am running the app on emulater and changing the battery level using telnet. It changes the battery level but not showing any toast or logs.
我正在模拟器上运行该应用程序并使用 telnet 更改电池电量。它会更改电池电量但不显示任何吐司或日志。
What am I missing?? Any help is appreciated! Thank you.
我错过了什么??任何帮助表示赞赏!谢谢你。
采纳答案by Yahor10
Register your receiver in the code, not in the AndroidManifest
file.
在代码中而不是在AndroidManifest
文件中注册您的接收器。
registerReceiver(batteryChangeReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED)); // register in activity or service
public class BatteryChangeReceiver extends BroadcastReceiver {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
}
}
unregisterReceiver(batteryChangeReceiver);//unregister in the activity or service
Or listen to the battery level with null
receiver.
或使用null
接收器收听电池电量。
Intent BATTERYintent = this.registerReceiver(null, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Log.v(null, "LEVEL" + level);
回答by k3v
You can register your receiver in the AndroidManifest.xml
, however make sure that the action you are filtering on is
您可以在 中注册您的接收器AndroidManifest.xml
,但请确保您过滤的操作是
android.intent.action.BATTERY_LOW
android.intent.action.BATTERY_LOW
and not
并不是
android.intent.action.ACTION_BATTERY_LOW
android.intent.action.ACTION_BATTERY_LOW
(which you have used in your code).
(您在代码中使用过)。
回答by EZDsIt
k3v is correct.
k3v 是正确的。
There is actually an error in the documentation.
It specifically says to use android.intent.action.ACTION_BATTERY_LOW
.
But the correct action to put in the manifest is android.intent.action.BATTERY_LOW
See here: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
文档中实际上存在错误。它特别说要使用android.intent.action.ACTION_BATTERY_LOW
. 但放入清单的正确操作android.intent.action.BATTERY_LOW
见此处:http: //developer.android.com/training/monitoring-device-state/battery-monitoring.html
(Couldn't vote k3v's answer up, not enough StackOverflow point things...)
(无法对 k3v 的回答进行投票,StackOverflow 点的东西还不够......)
UPDATE: I now can and did up-vote k3v's answer :-)
更新:我现在可以并且对 k3v 的回答投了赞成票 :-)
回答by seb
Maybe the emulator does not respond to BATTERY_LOW
and BATTERY_OKAY
messages. Try on a real Android device.
也许模拟器没有响应BATTERY_LOW
和BATTERY_OKAY
消息。在真正的 Android 设备上试用。
回答by chris
Register using context register. If you are targeting Android 8.0 or higher, you cannot use manifest declared receiver. Paste this code in your main activity to register.
使用上下文 register 进行注册。如果您的目标是 Android 8.0 或更高版本,则不能使用清单声明的接收器。将此代码粘贴到您的主要活动中以进行注册。
BroadcastReceiver receiver = new BatteryLevelReceiver();
IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_LOW);
this.registerReceiver(receiver, filter);
and you are good to go PSthe emulator should not be in charging state
你很高兴去 PS模拟器不应该处于充电状态