简体   繁体   中英

Android broadcastReceiver issue power connection

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM