0

I'm trying to receive a broadcast when power source connects or disconnects to the device. So I mention the receiver in the manifest file but it doesn't work. But When I declare the broadcast dynamically it works fine.

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "POWER CONNECTION CHANGED", Toast.LENGTH_LONG).show();

    }
}

manifest code snippet

  <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>
  • https://developer.android.com/guide/components/broadcast-exceptions – CommonsWare Jun 11 '20 at 10:47
  • then What should I do to achieve the same kind of functionality?? – Sahil Khatri Jun 11 '20 at 10:59
  • you should really read, check commonsware comment link, you can no longer register those type broadcast via manifest. You need to use a context-based approach. – hemen Jun 11 '20 at 11:04
  • Does this answer your question? [Android: Event ACTION\_POWER\_CONNECTED is not sent to my BroadcastReceiver](https://stackoverflow.com/questions/2104372/android-event-action-power-connected-is-not-sent-to-my-broadcastreceiver) – hemen Jun 11 '20 at 11:04

0 Answers0