电池状态始终不充电 [英] Battery status is always not charging
本文介绍了电池状态始终不充电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
if (status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL)
Toast.makeText(context, "Charging!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Not Charging!", Toast.LENGTH_SHORT).show();
}
清单:
<receiver android:name=".receiver.BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
在此code,面包总是显示未充电!我测试了这个在实际设备上,当我将其插入交流电源或USB电源,但它仍然显示不在充电!吐司。
In this code, the Toast always shows "Not Charging!". I tested this on an actual device, and when I plug it into AC or USB power, it still displays the "Not Charging!" Toast.
推荐答案
您无法通过该清单为 ACTION_BATTERY_CHANGED
注册,这样你就不会接受那些广播。您正在尝试从意图
没有那些群众演员(如获得
)。因此,你所得到的默认值 BatteryManager
演员ACTION_POWER_CONNECTED BATTERY_STATUS_UNKNOWN
。
You cannot register for ACTION_BATTERY_CHANGED
via the manifest, so you are not receiving those broadcasts. You are trying to get BatteryManager
extras from Intents
that do not have those extras (e.g., ACTION_POWER_CONNECTED
). As a result, you are getting the default value of BATTERY_STATUS_UNKNOWN
.
这篇关于电池状态始终不充电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文