Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android MTP设备中检测USB连接?_Android - Fatal编程技术网

如何在Android MTP设备中检测USB连接?

如何在Android MTP设备中检测USB连接?,android,Android,我需要能够在基于MTP的Android设备上检测USB连接,也就是当它被插入和断开时。这是我目前的相关代码,它是基于 manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.waracle.ncr" android:versionCode="1"

我需要能够在基于MTP的Android设备上检测USB连接,也就是当它被插入和断开时。这是我目前的相关代码,它是基于

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.waracle.ncr"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <uses-feature 
        android:name="android.hardware.usb.host"
        />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.example.TestActivity"
            android:label="@string/app_name" >

            <intent-filter>             
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
                <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
                <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter"/>
            </intent-filter>
        </activity>     

    </application>

</manifest>
这是广播接收器

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {    
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(TestActivity.this, "Test broadcastreceiver Received something", Toast.LENGTH_SHORT).show();
    }
};
这应该让我干杯如果它收到任何东西,什么都不会发生


我四处寻找解决方案,但我发现一切可行的都是基于UMS的。如果有人能帮上忙,那就太好了,谢谢。

您解决问题了吗?
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {    
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(TestActivity.this, "Test broadcastreceiver Received something", Toast.LENGTH_SHORT).show();
    }
};