1

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

Nazneen
  • 266
  • 3
  • 16

1 Answers1

0

please have a look on this link (The answer given by me)

activate an application when a power button is clicked

this will help you to solve your problem.

Community
  • 1
  • 1
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • 1
    I tried what you had suggested in your answer..but my problem still persists. I want to trigger a particular code when the device is set to charge through USB cable or AC...The receiver is just not responding to the event of plugging the USB cable...I cannot figure out what is going wrong! – Nazneen Jan 23 '12 at 18:02