【问题标题】:Detect USB Device Disconnection检测 USB 设备断开连接
【发布时间】:2013-12-20 14:13:29
【问题描述】:

我正在尝试检测 USB 设备断开连接。我正在使用下面的代码:

BroadcastReceiver receiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(
                    UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                Toast.makeText(getBaseContext(), "usb was disconneced",
                        Toast.LENGTH_LONG).show();
            }
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(receiver, filter);

但它不起作用。当我断开 USB 存储设备时没有任何反应。有什么问题?

谢谢。

【问题讨论】:

    标签: android usb broadcastreceiver


    【解决方案1】:

    您可以接收电源连接或断开的事件,例如通过将其放在 android manifest.xml 中:

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

    更多关于USB连接和断开的详细说明请关注example

    【讨论】:

    • 我之前试过这个例子。它没有用。我会给你的“ACTION_POWER_DISCONNECTED”建议一个机会。
    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多