I want to receive an interrupt when the android device starts charging either thr USB or AC I have registered the receiver through the following code--
registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
The code which accepts the broadcast is--
public void onReceive(Context context, Intent intent) {
powerMonitor = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
{
msg = "power connected";
}
The issue is when I connect the device through a USB cable I fail to get the interrupt and if I try to remove and plug the USB cable,the VM gets disconnected..How do I check it? Have I gone wrong? I even tried d following code--
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(broadcastReceiver, filter);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged==BatteryManager.BATTERY_PLUGGED_AC ||plugged==BatteryManager.BATTERY_PLUGGED_USB)
{
msg="power connected"
}
Please help!!