Getting a USB cable connected to IN / OUT using EXTRA_PLUGGED does not work

My intention is to keep the usb / power cable Android device settings in the current state: connected / disconnected. From the developer's site, I see that there are two intentions for obtaining this status: ACTION_POWER_CONNECTED / DISCONNECTED. Therefore, I used the same code as in Developers:

http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

under "Changes to the monitor while it is charging."

manifest

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

Java code

 public class PowerConnectionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; Toast.makeText(context, "pvr = " + usbCharge + ", " + acCharge + "," + isCharging, Toast.LENGTH_SHORT).show(); } } 

When I plug in the USB IN / OUT cable and the broadcast is always sent correctly and gets into the PowerConnectionReceiver, but always with the same result (= cable connected).

I tested it with Galaxy Nexus 4.1.1 / 4.2.1. I always get 2xFALSE in Toast (chargePlug = FALSE, usbCharge = FALSE).

Why does intent.getIntExtra (BatteryManager.EXTRA_PLUGGED, -1) always return the default value of -1?

Thanks.

ps. everything works fine if I register the receiver in Java

 Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

... in service or in action. But, according to the Android developer site, there is no need to do the code above to return the correct value;

+6
source share
3 answers

I was dealing with the same problem going through your post. I think the problem is that the code on the Android training page that you provided the link to is incorrect.

ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED do not "transfer" the data that you request with the intent of your code. I couldn’t get the same material to work on my devices (1st Nexus 7 gene running Android 4.3 and Nexus One running Android 2.3.6), and since it seems to be working nowhere, I came up with "bad code, "conclusion.

I fixed it with the following code. This is actually very close to what you provided as a fix, except that it is directly included in my BroadcastReceiver and not in action. In your code above, the snippet here will go immediately before the start of the line "int chargePlug". After that, change the "intention" on the same line (your line), starting from "int chargePlug", to "mIntent".

 final Intent mIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

Remember that your BroadcastReceiver will now have two intentions. One of them passed, which will be the target containing the ACTION_POWER_CONNECTED (or ACTION_POWER_DISCONNECTED) action, and the other created in BroadcastReceiver, explicitly to extract battery information.

The code snippet that I provided will not work if you put it into action (it will not connect to your BroadcastReceiver) because of the null in the parameter list. You are not actually registering a BroadcastReceiver with this zero. Changing the null value to your BroadcastReceiver would not be ideal, since an ACTION_BATTERY_CHANGED notification could trigger a LOT. I am pretty sure that ACTION_BATTERY_CHANGED was not intended to be used as a trigger for BroadcastReceiver. Instead, I think it means that it is used in real time to get the latest “sticky” broadcast with information about changing battery information (see the “sticky broadcast” in the Android documentation).

Please note that the above snippet includes "getApplicationContext ()" in the call chain. Take it and the Gingerbread devices will be broken (thanks CommonsWare).

+8
source

I am not sure why you have problems, but for me the registration of ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED actions worked for me:

 <receiver android:name="PowerReceiver" > <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> </receiver> 

And implementing the broadcast receiver as follows:

 public class PowerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction() == Intent.ACTION_POWER_CONNECTED) { //Handle power connected } else if(intent.getAction() == Intent.ACTION_POWER_DISCONNECTED){ //Handle power disconnected } } } 
+2
source
 public class PowerConnectionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final Intent mIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int status = mIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; int chargePlug = mIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BATTERY_PLUGGED_AC; } } 
+1
source

All Articles