gpt4 book ai didi

android - 如何接收USB连接状态广播?

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:24 28 4
gpt4 key购买 nike

我正在尝试在我的应用程序中检测 USB 连接,即 USB 是否已连接到设备。

正在 Marshmallow 6.0.1 (sdk23) 上测试

但我无法接收广播操作 ACTION_USB_DEVICE_ATTACHED 或 ACTION_USB_DEVICE_DETACHED..

我尝试同时使用动态方式和 AndroidManifest.xml 方式,都没有用..

这是我的代码:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gokulnc.blah_blah"
android:installLocation="auto"
android:versionCode="15"
android:versionName="1.5.1">

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

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />

<android:uses-permission android:name="android.permission.USB_PERMISSION" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:vmSafeMode="false">

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/DrawerTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<receiver android:name="mUsbReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
</receiver>
</activity>
</application>

</manifest>

MainActivity.java :

public class MainActivity extends AppCompatActivity {
BroadcastReceiver mUsbReceiver;

public void onCreate(Bundle savedInstanceState) {
.....
setBroadcastReceivers();
}

void setBroadcastReceivers() {
//Reference: http://www.codepool.biz/how-to-monitor-usb-events-on-android.html

mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(LOG_TAG, "Received Broadcast: "+action);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action) || UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {

updateUSBstatus();
Log.d(LOG_TAG, "USB Connected..");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action) || UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
updateUSBstatus();
}
Log.d(LOG_TAG, "USB Disconnected..");
}
}
};

IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
//filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
//filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver , filter);
Log.d(LOG_TAG, "mUsbReceiver Registered");
}

@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "App Resumed..");
//Refernce: https://stackoverflow.com/questions/18015656/cant-receive-broadcast-intent-of-usbmanager-action-usb-device-attached-usbmanag
Intent intent = getIntent();
if (intent != null) {
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
Toast.makeText(getApplicationContext(), "Attached", Toast.LENGTH_SHORT).show();
} else if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Toast.makeText(getApplicationContext(), "Detached", Toast.LENGTH_SHORT).show();
}
}
}
}

我还检查了这个答案:Can't receive broadcast Intent of UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED , 但它没有帮助..

有人可以指出我哪里错了吗?

最佳答案

可能它不起作用的原因是,从 Android 6.0 开始,默认的 USB 模式是充电,并且在该模式下连接时,可能 ACTION_USB_DEVICE_ATTACHED 不会启动。

相反,现在我有了另一个解决方案:

String usbStateChangeAction = "android.hardware.usb.action.USB_STATE";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(LOG_TAG, "Received Broadcast: "+action);
if(action.equalsIgnoreCase(usbStateChangeAction)) { //Check if change in USB state
if(intent.getExtras().getBoolean("connected")) {
// USB was connected
} else {
// USB was disconnected
}
}
}

也就是说,只要 USB 状态发生切换,就会发送广播 android.hardware.usb.action.USB_STATE

不像 android.hardware.usb.action.USB_DEVICE_ATTACHED 仅在启用 MTP 模式时广播,android.hardware.usb.action.USB_STATE 是每当它检测到能够连接到主机(比如计算机)的 USB 连接时广播,而不管其当前的 USB 模式,如充电、MTP 或其他任何模式。(更准确地说,它称为 USB 配置)

关于android - 如何接收USB连接状态广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942453/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com