Android broadcastReceiver issue power connection
<p>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--</p>

<pre><code>registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
</code></pre>

<p>The code which accepts the broadcast is--</p>

<pre><code>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";
}
</code></pre>

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

<pre><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"
}
</code></pre>

<p>Please help!!</p>
