How to get privileges like restricted broadcasts for my root app?

Viewed 42

How can I make my app get restricted broadcasts after android 8+ using root commands.

I want toast every time user connects device to a charger or disconnect the device. I have tried JobScheduler but It doesn't seem to work properly.

1 Answers

Monitor the charging status change.

The charging status can easily be changed (inserting/removing the charger), so it is important to monitor the charging status and change the refresh rate.

When the charging status changes, the BatteryManager sends a broadcast. It is important to receive these events, even when the app is not running, because you may need to enable the update service in the background. Therefore, you need to register the broadcast receiver in the Androidmanifest.xml file and add two actions: ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED for filtering.

Sample code:

<receiver android:name=".PowerConnectionReceiver">

  <intent-filter>

    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>

    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>

  </intent-filter>

</receiver>

In the associated broadcast receiver implementation, you can read out the current charging status.

Sample code:

public class PowerConnectionReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) { 

        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;

    }

}
Related