BroadCast Battery Charging state

//onCreate

IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
getApplicationContext().registerReceiver(mBroadcastReceiver,iFilter);





private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Received", Toast.LENGTH_SHORT).show();
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
if(isCharging){
textview1.setText("Charging : Yes.");
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
if(usbCharge){
textview1.setText(textview1.getText()+"
USB Charging");
}
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(acCharge){
textview1.setText(textview1.getText()+"
AC Charging");
}
boolean wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
if(wirelessCharge){
textview1.setText(textview1.getText()+"
Wireless Charging");
}
}else {
textview1.setText("Charging : No.");
}
}
};

Komentar

Cara pembuatan Apps paling Populer

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

How to find and​ highlight a word in a text field in Sketchware?

A Flash Light App in Sketchware

Create Stopwatch App in Android using Sketchware

Code for implementing Notifications in Sketchware

How to share an image from Drawable folder?

Code for implementing Notifications in Sketchware

Firebase Login/Register with email verification