我试图阻止我正在处理的应用程序每次USB设备断开时都要求USB权限.我跟着:USB设备访问弹出抑制?没有太多运气.
该应用确实记住USB设备,直到设备被拔掉.
如果重要,我正在尝试将应用程序连接到Arduino Teensy.
这就是我的清单活动的样子
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter> <!-- For receiving data from another application -->
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</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/usb_device_filter" />
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
这是我的usb_device_filter文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="--hidden--" product-id="--hidden--" />
</resources>
Run Code Online (Sandbox Code Playgroud)
欢迎任何帮助.
编辑:我也查看Android Developer页面,他们说当使用意图过滤器进行USB连接时:如果用户接受,您的应用程序将自动拥有访问设备的权限,直到设备断开连接.
我还想指出,建议的副本与我试图解决的问题不同.他们的问题是有两个活动,而我的问题只涉及一个.
https://developer.android.com/guide/topics/connectivity/usb/host
我们最终在 USB 插入手机时自动打开应用程序,因为我们的项目处理全天打开的应用程序。
该应用程序只要求手机使用一次 USB 权限,并且现在会记住它,只要用户在打开应用程序之前连接 USB 并且手机已解锁。手机重启后甚至会记住该权限。
这是一种解决方法,但它可以满足我们的需求。希望这能帮助其他遇到同样问题的人。