Extreme Hacking | Sadik Shaikh
Advanced Ethical Hacking Institute Course in Pune

In the Mobile world , battery is the most important factor for calculating performance or quality of mobile device. Because without battery or power no device will run so we should know about battery and how to optimize for better usage of power. Here in this article I would like to show how to get battery information from android. In Android we are having Broadcast receiver concept. Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. So through broadcast receiver we can get battery information from android. We have to declare broadcast receiver in manifest file or else in code itself. Here is the code for declaring broadcast receiver

private final BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
}
};

Yes we declared broadcast receiver in our code but this not specifying what kind of message it will receive. Here is code for specifying message of related to battery information. This code it should be inonCreate() method.

IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);

Now we have one broadcast receiver and we know what message it will send. Now we have to register and unregister the broadcast receiver. Here is the code for register and unregister.

@Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(batteryReceiver);
    }

@Override
public void onResume() {
    super.onResume();
    registerReceiver(batteryReceiver, mIntentFilter);
    // buildChart();
}

Then when we executing the code we will get battery information through intent in onReceive(Context context, Intent intent). Here is the sample code for getting all Battery information from intent object.

private final BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();
       if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {

            int mLevel = intent.getIntExtra("level", 0);
            int mtrScale = intent.getIntExtra("scale", 0));
            int mVoltage = intent.getIntExtra("voltage", 0);
            int temperature = intent.getIntExtra("temperature", 0);
            String strTechnology = intent.getStringExtra("technology"));
            int status = intent.getIntExtra("status",
            BatteryManager.BATTERY_STATUS_UNKNOWN);

            //Checking Battery is charging or not
            switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:

            String statusString = context.
            getString(R.string.battery_info_status_charging);

                int plugType = intent.getIntExtra("plugged", 0);

                if (plugType > 0) {
                    statusString = statusString + " " + context                                 
               .getString((plugType == BatteryManager.BATTERY_PLUGGED_AC) ?                 
                R.string.battery_info_status_charging_ac                                      
               : R.string.battery_info_status_charging_usb);
                }

                mStatus.setText(statusString);
                break;

            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                mStatus.setText(R.string.battery_info_status_discharging);
                break;

            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                mStatus.setText(R.string.battery_info_status_not_charging);
                break;

            case BatteryManager.BATTERY_STATUS_FULL:
                mStatus.setText(R.string.battery_info_status_full);
                break;

            default:
                mStatus.setText(R.string.unknown);
                break;

            }

          //Here we are getting health condition of battery

            switch (intent.getIntExtra("health",
                    BatteryManager.BATTERY_HEALTH_UNKNOWN)) {

            case BatteryManager.BATTERY_HEALTH_GOOD:
                mHealth.setText(R.string.battery_info_health_good);
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                mHealth.setText(R.string.battery_info_health_overheat);
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD:
                mHealth.setText(R.string.battery_info_health_dead);
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                mHealth.setText(R.string.battery_info_health_over_voltage);
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                mHealth.setText(R.string.battery_info_health_unspecified_failure);
                break;
            default:
                mHealth.setText(R.string.unknown);
                break;
            }
    }
};

Here is the Complete Code github

www.extremehacking.org
CEHv9 CHFI ECSAv9 CAST ENSA CCNA CCNA SECURITY MCITP RHCE CHECKPOINT ASA FIREWALL VMWARE CLOUD ANDROID IPHONE NETWORKING HARDWARE TRAINING INSTITUTE IN PUNE,Certified Ethical Hacking, Center For Advanced Security Training in India,IT Security Training Information Security Traning Courses in Pune, ceh certification in pune, Ethical Hacking Course in Pune