Is there any way to control charging in android programatically without using any third party Apps?

673 views Asked by At

We have an Android Application which has to be installed at Client Location in Tab and the tab has to be charged and running continuosly without getting tab turn off which leads to battery health drop. So We have to implement a solution in our app where the user can set some limit to charging to control even if the charger is connected physically.

(Example: If i set some 80% as higher limit and 20% as lower limit then the tab should get stop charging when it reaches to 80% and start charging when it reaches to 20% automatically).

Can someone Please tell me Is there any way to do this.

Thanks in advance.

2

There are 2 answers

0
Avinash Tilekar On

You may need to use BroadCastReceiver with intent-filter on BroadcastReceiver try to get battery Level via BatteryManager:

public class MyPowerMonitorReceiver extends BroadcastReceiver {
  
    private float currentBatteryLevel;

    @Override
    public void onReceive(Context context, Intent intent) {



       switch (action) {
            case (Intent.ACTION_BATTERY_CHANGED): {
                Log.i(TAG, "Battery has changed");

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

                currentBatteryLevel = (level * 100) / (float) scale;

                Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%" + " level: " + level + " scale: " + scale, Toast.LENGTH_LONG).show();

                break;
            }
            case (Intent.ACTION_POWER_CONNECTED): {
                Log.i(TAG, "Power connected");

                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;

                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

                String chargingType = null;

                if (usbCharge) {
                    chargingType = "USB";
                } else if (acCharge) {
                    chargingType = "AC Power";
                }

                if (isCharging && chargingType != null) {
                    Toast.makeText(context, "Device is charging via: " + chargingType, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Device is charging.", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case (Intent.ACTION_POWER_DISCONNECTED): {
                Log.i(TAG, "Power disconnected");
                Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
                break;
            }
            case (Intent.ACTION_BATTERY_LOW): {
                Log.i(TAG, "Battery low");
                Toast.makeText(context, "Battery low", Toast.LENGTH_SHORT).show();
                break;
            }
            case (Intent.ACTION_BATTERY_OKAY): {
                Log.i(TAG, "Battery Okay");
                Toast.makeText(context, "Battery Okay", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
}
0
RatherBeSailing On

Getting the battery level is easy

val bm = getSystemService(BATTERY_SERVICE) as BatteryManager
val batteryPercentage = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)

Unfortunately I have had no luck with controlling charging

Would love to be proved wrong !