Skip to content Skip to sidebar Skip to footer

Obtaining Usb Cable Plugged In/out Event Using Extra_plugged Does Not Work

My intention is to have saved in Preferences current status of Android device usb/power cable: connected/disconnected. From Developer site I see that there are two Intent for obtai

Solution 1:

I was dealing with the same problem, coming across your post. I think the issue is that the code, in the Android training page to which you provided a link, is wrong.

ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED aren't "carrying" the data that you're querying from the intent in your code. I couldn't get that same stuff to work on my devices either (1st gen Nexus 7 running Android 4.3 and Nexus One running Android 2.3.6) and, because it seems to not be working anywhere, I come to the "bad code" conclusion.

I fixed it with the following code. It's actually very close to what you've provided as a fix except that it's going into my BroadcastReceiver directly and not into an activity. In your code above, the snippet here would go just before the line beginning "int chargePlug". After that, change "intent" in that same line (your line), beginning "int chargePlug", to "mIntent".

finalIntentmIntent= context.getApplicationContext().registerReceiver(null, newIntentFilter(Intent.ACTION_BATTERY_CHANGED));

Be aware that your BroadcastReceiver will now have two intents. One passed to it, which will be the intent containing the action ACTION_POWER_CONNECTED (or ACTION_POWER_DISCONNECTED), and the other created within the BroadcastReceiver expressly to extract battery information.

The code snippet that I've supplied would not work if you placed it into an activity (it wouldn't connect to your BroadcastReceiver) because of the null in the parameter list. You're not actually registering a BroadcastReceiver with that null. Changing the null to your BroadcastReceiver wouldn't be ideal either since the ACTION_BATTERY_CHANGED notification can trigger A LOT. I'm quite confident that ACTION_BATTERY_CHANGED wasn't intended to be used as a trigger for a BroadcastReceiver. Instead, I think it's meant to be used in real time to get the last "sticky" broadcast with information about a change in battery info (look up "sticky broadcast" in the Android documentation).

Note also that the snippet I've given includes "getApplicationContext()" in the call chain. Take it out and Gingerbread devices will crash (thanks CommonsWare).

Solution 2:

I'm not sure why you're having problems but what worked for me was registering the for the ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED actions:

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

And implementing the broadcast receiver like this:

publicclassPowerReceiverextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context context, Intent intent) {
        if(intent.getAction() == Intent.ACTION_POWER_CONNECTED) {
            //Handle power connected
        } elseif(intent.getAction() == Intent.ACTION_POWER_DISCONNECTED){
            //Handle power disconnected
        }
    }
}

Solution 3:

publicclassPowerConnectionReceiverextendsBroadcastReceiver {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        finalIntentmIntent= context.getApplicationContext().registerReceiver(null, newIntentFilter(Intent.ACTION_BATTERY_CHANGED));

        intstatus= mIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        booleanisCharging= status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;


        intchargePlug= mIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        booleanusbCharge= chargePlug == BATTERY_PLUGGED_USB;
        booleanacCharge= chargePlug == BATTERY_PLUGGED_AC;

    }
}

Post a Comment for "Obtaining Usb Cable Plugged In/out Event Using Extra_plugged Does Not Work"