Android: How to get Battery level status more accurately?

The main objective of this post is to help you to get all the details of the Battery in Android. Create a BroadcastReceiver in your activity to get all value of battery. double batteryCapacity;

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
        mTextViewInfo.setText("Battery Scale : " + scale);

        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);

        // Display the battery level in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);

        // Calculate the battery charged percentage
        float percentage = level/ (float) scale;

        //Calculate the battery remain level percentage
        int remainBattery=scale-level;

        // Update the progress bar to display current battery charged percentage
       int batteryPer= (int)((percentage)*100);

        //show the battery remaining percentage in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() + "\nRemain Battery :"+ remainBattery +"%");


        //Current Battery Temperature in TextView
        int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
        if (temperature > 0) {
             temp = ((float) temperature) / 10f;
            mTextViewInfo.setText(mTextViewInfo.getText() + "\nCurrent Temperature :"+ temp+"ÂșC");
        }

        //Battery Voltage.
        int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
        if (voltage > 0) {
            System.out.println("<<< battery volt "+voltage);
        }



//For BatteryLIfe.
        getBatteryCapacity(context);

  }
};
In OnCreate register your broadcast .

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

    mContext = getApplicationContext();

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

    IntentFilter currTime = new IntentFilter(Intent.ACTION_TIME_TICK);
    mContext.registerReceiver(mBroadcastReceiver, currTime);

    IntentFilter i=new IntentFilter(Intent.ACTION_POWER_USAGE_SUMMARY);
    mContext.registerReceiver(mBroadcastReceiver,i);
}
and To get your battery capacity the code is.
public void getBatteryCapacity(Context mContext) {
    Object mPowerProfile_ = null;

    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS).getConstructor(Context.class).newInstance(mContext);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
         batteryCapacity = (Double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getAveragePower", java.lang.String.class)
                .invoke(mPowerProfile_, "battery.capacity");
    } catch (Exception e) {
        e.printStackTrace();
    }
}