Search code examples
androidandroid-intentbroadcastreceiverintentfilter

Android listen for ACTION_DOCK_EVENT


I am trying to listen for dock change events per this documentation and ACTION_DOCK_EVENT.

My receiver is never hit.

My code seems pretty simple, so I'm wondering is there a permission that is required for listening to dock events? Am I missing something else related to ACTION_DOCK_EVENT?

AndroidManifest.xml

<receiver android:enabled="true" android:exported="true" android:label="MyReceiver" android:name="path.to.MyReceiver">
  <intent-filter>
    <action android:name="android.intent.action.DOCK_EVENT"/>
  </intent-filter>
</receiver>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == Intent.ACTION_DOCK_EVENT) {
      //Never hit :(
    }
  }

}

I am testing by plugging / unplugging my phone into a my AC power outlet and macbook pro. I am using a Moto X (2nd Genration) with Android 4.4.4.


Solution

  • Docks are a very specific type of accessory as seen in the types of docks:

    • Car
    • Desk
    • Low-End (Analog) Desk
    • High-End (Digital) Desk

    You'll note that none of these correspond with being plugged into a power source. For that, you'd probably want to monitor changes in charging state using an Intent filter such as

    <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>