【问题标题】:Can't detect usb cable on my mobile Application无法在我的移动应用程序上检测到 USB 电缆
【发布时间】:2018-04-09 13:20:28
【问题描述】:

我已经创建了一个 android 应用程序,如果 USB 连接到 PC,它会告诉将文本“mtp”设置为 textview,或者如果 USB 连接到充电器,则将文本“充电器”设置为 textview。我使用广播接收器进行 USB 电缆检测! 当我在手机上运行应用程序并将 USB 数据线连接到电脑或充电器时,什么也没有发生,它只打印无!问题出在哪里?

public class Receiver extends BroadcastReceiver
{
    Intent intent;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        intent = new Intent("check");

        if(intent.getAction().equalsIgnoreCase("android.hardware.usb.action.USB_STATE"))
        {
            intent.putExtra("MTP", true);
            context.sendBroadcast(intent);
        }
        else if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
        {
            intent.putExtra("isChargerConnected", true);
            context.sendBroadcast(intent);
        }
        else
        {
            intent.putExtra("isCableConnected", false);
            context.sendBroadcast(intent);
        }
    }
}

public class MainActivity extends AppCompatActivity
{
    private TextView cable;
    BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cable = (TextView) findViewById(R.id.cable);

        broadcastReceiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                if (intent != null && intent.getAction().equals("check"))
                {
                    Bundle bundle = intent.getExtras();
                    if (bundle != null && bundle.containsKey("MTP"))
                    {
                        boolean isUSBConnected = bundle.getBoolean("MTP");

                        if(isUSBConnected)
                            cable.setText("MTP");
                    }
                    if (bundle != null && bundle.containsKey("isChargerConnected"))
                    {
                        boolean isChargerConnected = bundle.getBoolean("isChargerConnected");

                        if(isChargerConnected)
                            cable.setText("Charger");
                    }
                    if (bundle != null && bundle.containsKey("isCableConnected"))
                    {
                        boolean isCableConnected = bundle.getBoolean("isCableConnected");

                        if(!isCableConnected)
                            cable.setText("None");
                    }
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter("check");
        registerReceiver(broadcastReceiver, intentFilter);
    }

        <receiver android:name=".Receiver">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_STATE"/>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            </intent-filter>
        </receiver>

【问题讨论】:

    标签: android usb


    【解决方案1】:

    你可以试试这个代码

    <activity
    ...
    android:launchMode="singleTask">
    

    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
    if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
    {
        UsbDevice device  = 
    (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    

    【讨论】:

    • 我将 android:launchMode="singleTask" 添加到 MainActivity 但仍然只显示“None”!
    • 还是同样的问题!
    • 您绝对应该添加洞察力哦,为什么这是有效的。 试试这个很少是一个足够好的答案。
    【解决方案2】:

    首先,使用以下代码检查设备是否正在充电(USB 或 AC):

    public boolean isDeviceCharging(){
       return  myBatteryManager.isCharging();
    }
    

    然后使用以下代码检查 USB 是否已连接或交流充电器:

    public boolean checkUSB(){
        Intent intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
        return intent.getExtras().getBoolean("connected");
    }
    
    private  boolean checkACCharger()
    {
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        chargePercentage = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
        return  acCharge;
    }
    

    您还可以使用以下函数获取电池电量百分比:

    public  int getBatteryCharge() {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        return  level;
    }
    

    PS:BatteryManager.isCharging() 方法可用于 API 级别 23 及以上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多