How to detect power connected state?

In AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".receiver.PlugInControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>

In Code

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

       if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
           // Do something when power connected
       }
       else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
           // Do something when power disconnected
       }
   }
}

Leave a Comment