Obtaining usb cable plugged IN/OUT event using EXTRA_PLUGGED does not work

给你一囗甜甜゛ 提交于 2019-12-01 05:22:22

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

final Intent mIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(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).

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:

<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 like this:

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

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!