0

I have been trying to use reflection in case when anybody plug in charger or usb cable to charge their device. Can anybody please tell me what does android implements to interact with the hardware.

I am not exactly interested in the USB I am interested more in the red(amber in case of HTC) led that glow when we connect our device for charging or in case of notification.

Mihir
  • 2,064
  • 2
  • 21
  • 28

2 Answers2

2

Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED. An Intent extra will tell you what the charging state is -- see BatteryManager for details.

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

Then

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
    }
}
Arun C
  • 9,035
  • 2
  • 28
  • 42
0

see this link

you can monitor battery charging state by registering following intent filter

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

and in code

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                            status == BatteryManager.BATTERY_STATUS_FULL;
    }
}
stinepike
  • 54,068
  • 14
  • 92
  • 112