-4

I purchased Lenovo K8 note mobile which has stock android Nougat 7.1.1 . i am trying to display the battery percentage instead of graphical display on the top notification bar. I found no setting to do the same. Please help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vinit
  • 3

1 Answers1

1

Use below code for getting battery percentage;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int voltage = intent.getIntExtra("voltage", 0);
        int temperature = intent.getIntExtra("temperature", 0);
 //            batteryLevel.setText("Battery Status: " + String.valueOf(level) + "%");
 //            voltageLevel.setText("Battery Voltage: " + String.valueOf(voltage));
        double temps = (double)temperature / 10;
//            temperatureLevel.setText("Battery Temperature: " + String.valueOf(temps));

          Toast.makeText(getApplicationContext(),String.valueOf(level),Toast.LENGTH_SHORT).show();
    }
};
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57