0
我一直在試聽Battery BroadCast事件。插入/拔出。電池廣播不發送任何狀態代碼
public class BatteryReceiver extends BroadcastReceiver {
public BatteryReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
if(status == BatteryManager.BATTERY_STATUS_CHARGING){
Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
} else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING || status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
}
}
}
我已經加入清單的行動:
<receiver
android:name=".BatteryReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
第一個會爲我工作感謝,歡呼!但第二個總是返回-1 –
@GurleenSethi我更新了答案..現在,它正在工作 – W0rmH0le
@GurleenSethi您測試了更新的答案嗎?如果可能的話,讓我知道結果!謝謝 – W0rmH0le