BroadCast Battery Charging Status

//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);
String statusString = "";
if(status == BatteryManager.BATTERY_STATUS_UNKNOWN){
statusString = "UNKNOWN";
}else if(status == BatteryManager.BATTERY_STATUS_CHARGING){
statusString = "CHARGING";
}else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING){
statusString = "DISCHARGING";
}else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
statusString = "NOT CHARGING";
}else if (status == BatteryManager.BATTERY_STATUS_FULL){
statusString = "FULL";
}
textview1.setText("Battery Status : " + statusString);
}
};

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