Auto start an app when charging

android

Solution

To detect charging state, use BroadcastReceiver and register in your manifest file like ::

<receiver android:name=".broadcastReceiver">
  <intent-filter>
     <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
     <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

and to detect headphone is plugged or not use `AudioManager.isWiredHeadsetOn()`

Problem

I am creating an Android application that I want to auto-start as soon as the mobile is plugged into charging or headphone is plugged into it. Please provide any solution as to how to do it.

Original source

Related problems