Android的USB连接充电信号 [英] android USB connection charging signals

查看:221
本文介绍了Android的USB连接充电信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查USB插座插入或不(与AC / USB充电)? (android版2.1 / 2.2)

How can I check the USB socket is plugged or not (Charging with AC / USB)? (android version 2.1 / 2.2)

我写,我想获得不同类型的充电信号(无线,交流充电/ USB)无线充电的​​应用程序。我用的BroadcastReceiver得到充电信号,并发现,使用无线充电和交流充电返回相同的信号(AC充电)。
为了区分无线充电和交流充电,我要检测的USB接口插入与否。我尝试使用Intent.ACTION_UMS_CONNECTED检测USB信号,但它也返回SD卡。

I am writing a wireless charging application that I want to get different types of charging signals ( wireless , AC charging / USB ). I used broadcastReceiver to get the charging signal and found that using wireless charging and AC charging return the same signal (AC charging). To distinguish wireless charging and AC charging, I want to detect the USB socket is plugged or not. I try to use Intent.ACTION_UMS_CONNECTED to detect the USB signal but it also return SD card.

我如何检查USB插座插入或不(与AC / USB充电)? (android版2.1 / 2.2)

How can I check the USB socket is plugged or not (Charging with AC / USB)? (android version 2.1 / 2.2)

推荐答案

检查设备与AC / USB 充电,让我们试试这个,

To check device charging with AC / USB, Lets try this,

呼叫 registerReceiver(空,新的IntentFilter(Intent.ACTION_BATTERY_CHANGED))。这将返回对 BatteryManager 定义额外让你知道,如果它是在插入或不是意图。

Call registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)). This will return an Intent that has extras defined on BatteryManager to let you know if it is plugged in or not.

一些与code,

public class PowerUtil {
    public static boolean isConnected(Context context) {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }
}

这篇关于Android的USB连接充电信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆