Android - Broadcast Receivers

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action != null) {
            switch (action) {
                case Intent.ACTION_POWER_CONNECTED:
                    // Handle the power connected event
                    break;
                case Intent.ACTION_POWER_DISCONNECTED:
                    // Handle the power disconnected event
                    break;
                default:
                    // Handle other actions as needed
                    break;
            }
        }
    }
}
<manifest ...>
    <application ...>
        <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
Method Description
sendOrderedBroadcast(Intent, String) Sends broadcasts to one receiver at a time.
sendBroadcast(Intent) Sends broadcasts to all receivers in an undefined order.
localBroadcastManager.sendBroadcast(intent) Send Intent broadcasts to local objects within your process .This method is deprecated since API 28, and LiveData is used instead.