gpt4 book ai didi

Android:将设备USB插入/拔出USB端口时的BroadcastReceiver

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:11 25 4
gpt4 key购买 nike

我的应用程序没有捕捉到“ACTION_USB_DEVICE_ATTACHED”,但“ACTION_USB_DEVICE_DETACHED”工作正常。如果我连接 USB 设备但我在执行 BroadcastReceiver 时断开连接,应用程序会正确启动,正确捕获“ACTION_USB_DEVICE_DETACHED”。如果我再次附加 BroadcastReceiver,则不会捕获任何内容。

    IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
rocrailService.registerReceiver(mUsbReceiver, filter);


// BroadcastReceiver when insert/remove the device USB plug into/from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
mSerial.usbAttached(intent);
mSerial.begin(mBaudrate);
loadDefaultSettingValues();
Run=true;
start();
System.out.println("BroadcastReceiver USB Connected");

} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
mSerial.usbDetached(intent);
mSerial.end();
Run=false;
System.out.println("BroadcastReceiver USB Disconnected");
}
}

和 list :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.DCCWLocoDisplay"
android:versionCode="0"
android:versionName="0" >

<uses-sdk android:targetSdkVersion="12"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
android:icon="@drawable/dccw"
android:label="@string/app_name" >
<activity android:icon="@drawable/cab_64" android:name="net.DCCWLocoDisplay.activities.ActRCCab" android:theme="@style/FullHeightMainDialog" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
<activity android:name="net.DCCWLocoDisplay.activities.ActPreferences" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAccessory" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAbout" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActProgramming" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActSteps" android:theme="@style/FullHeightDialog" />
<service android:name="net.DCCWLocoDisplay.DCCWLocoDisplay"/>
</application>

</manifest>

设备过滤器(我检查供应商和产品 ID):

<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL -->
</resources>

最佳答案

一切都像上面描述的那样工作,除了一些小的变化!在我的研究中,应用程序标签中的 singleTask 没有帮助,但是当我把它放在 Activity 标签中时有帮助

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

它为我提供了 ANDROID 开始向 Activity 发送 onNewIntent。在 onNewIntent 上,我只需要从 Intent 中获取设备,而无需任何权限请求:

@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:将设备USB插入/拔出USB端口时的BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15172611/

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