i want to detect battery change at every percentage. I am able to detect battery level once with broadcast receiver. But its not updating value automatically when % change from 66 to 67.
Here is my code.
private void batteryLevel() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
Log.i("Battery CHANGED******",":: IN RECEIVER");
int rawlevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
btnConsumer.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
Added permission
<uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
It detect 66 but when it change 65 or 67 it will not update value. or not display log too
I want to make it like i can get every change.
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…