@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>
在這段代碼,麪包總是顯示 「不在充電!」。我在一臺實際的設備上測試過,當我插入交流電或USB電源時,它仍然顯示「不充電!」吐司。
那裏有什麼問題你intent.getIntExtra,檢查其中這些方法正在被呼叫,並確保你正在設置意圖的正確參數 – John 2012-07-22 18:24:22
你的地位是什麼? – zmbq 2012-07-22 18:24:53
@John我在清單中使用它。 – 2012-07-22 18:28:48