Android USB设备访问弹出抑制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12388914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:32:26  来源:igfitidea点击:

USB device access pop-up suppression?

androidusb

提问by Wayne Uroda

When a USB device is connected to the Android tablet, a pop-up appears asking for user-permission. I want to suppress this as the client does not want it. How should I go about that?

当 USB 设备连接到 Android 平板电脑时,会出现一个要求用户许可的弹出窗口。我想压制这一点,因为客户不想要它。我该怎么办?

In the code:

在代码中:

UsbManager.requestpermission(); 

is called to give the USB device temporary access.

被调用以授予 USB 设备临时访问权限。

This throws a pop-up. How do I suppress the pop-up or grant access to the user by default?

这会引发一个弹出窗口。默认情况下,如何禁止弹出窗口或授予用户访问权限?

回答by Wayne Uroda

When you request permission inside your app it seems that the checkbox "use by default for this USB device" does nothing (I am not sure why this checkbox even shows up on this popup.

当您在应用程序中请求权限时,“默认情况下为此 USB 设备使用”复选框似乎没有任何作用(我不确定为什么此复选框甚至显示在此弹出窗口中。

Instead you should register an intent handler for your activity in the manifest:

相反,您应该在清单中为您的活动注册一个意图处理程序:

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

You also have to create a filter file in your xml resources, eg res/xml/usb_device_filter:

您还必须在您的 xml 资源中创建一个过滤器文件,例如 res/xml/usb_device_filter:

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <usb-device vendor-id="26214" product-id="26214" />
</resources>

The vendor-id and product-id here have to be given in decimal - above both the VID and PID are 0x6666.

此处的供应商 ID 和产品 ID 必须以十进制形式给出 - VID 和 PID 均高于 0x6666。

What I have given above also works for a USB accessory (that is, where the accessory is the USB host and the android is the device) - in that case the intent-filter should register

我上面给出的也适用于 USB 配件(即,配件是 USB 主机,而 android 是设备) - 在这种情况下,意图过滤器应该注册

<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />

and you also have to include the meta data filter in exactly the same way.

并且您还必须以完全相同的方式包含元数据过滤器。

See http://developer.android.com/guide/topics/connectivity/usb/accessory.htmland search for the section "Using an intent filter".

请参阅http://developer.android.com/guide/topics/connectivity/usb/accessory.html并搜索“使用意图过滤器”部分。

EDIT

编辑

To conclude - if you register the intent-filter against your activity, the USB permission window will be displayed immediately when the USB device/accessory is connected. If the user checks the "use by default for this USB device" box and then grants permission, this will be remembered and the permission dialog will not be displayed again (unless the app is uninstalled or the user clears the default action from the application manager).

总而言之 - 如果您针对您的活动注册意图过滤器,则在连接 USB 设备/附件时将立即显示 USB 权限窗口。如果用户选中“默认为此 USB 设备使用”框然后授予权限,这将被记住,并且不会再次显示权限对话框(除非应用程序被卸载或用户从应用程序管理器中清除了默认操作) )。

I have put a tiny, terrible, working example project up here:

我在这里放了一个很小的、可怕的、有效的示例项目:

http://www.locusia.com/examples/permissionTest.zip

http://www.locusia.com/examples/permissionTest.zip

You will need to edit res/xml/usb_device_filter.xml, but otherwise this should allow you to test it out very quickly.

您将需要编辑 res/xml/usb_device_filter.xml,否则这应该允许您非常快速地对其进行测试。

For services...

对于服务...

It seems that a service cannot receive the USB intents. I got around this by making a hidden activity which would then re-broadcast the intents.

似乎服务无法接收 USB 意图。我通过制作一个隐藏的活动来解决这个问题,然后重新广播意图。

I define it in my manifest like this:

我在我的清单中定义它是这样的:

<activity
    android:name=".activities.UsbEventReceiverActivity"
    android:label="YOUR APPLICATION NAME - This appears in the permission popup"
    android:theme="@style/Theme.Transparent" 
    android:noHistory="true"
    android:excludeFromRecents="true"
    android:taskAffinity="com.example.taskAffinityUsbEventReceiver"
    android:process=":UsbEventReceiverActivityProcess"
    android:exported="false"
    >    
    <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>

(I have a complex task/process layout in my service, YMMV in that area).

(我的服务中有一个复杂的任务/流程布局,该区域中有 YMMV)。

I defined the activity like this:

我定义了这样的活动:

public class UsbEventReceiverActivity extends Activity
{   
    public static final String ACTION_USB_DEVICE_ATTACHED = "com.example.ACTION_USB_DEVICE_ATTACHED";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        Intent intent = getIntent();
        if (intent != null)
        {
            if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
            {
                Parcelable usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                // Create a new intent and put the usb device in as an extra
                Intent broadcastIntent = new Intent(ACTION_USB_DEVICE_ATTACHED);
                broadcastIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);

                // Broadcast this event so we can receive it
                sendBroadcast(broadcastIntent);
            }
        }

        // Close the activity
        finish();
    }
}

And the last piece of the puzzle, the transparent theme (I'm not sure but you could probably use the built in android translucent theme) - res/values/styles.xml:

最后一块拼图,透明主题(我不确定,但您可能会使用内置的 android 半透明主题)- res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>  
    <resources>  
    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>  
</resources>  

回答by jlbofh

Late but i hope to help. Official android docssays "When users connect a device that matches your device filter, the system presents them with a dialog that asks if they want to start your application. If users accept, your application automatically has permission to access the device until the device is disconnected.". If you also check box to remember device, permission is granted until app uninstalls or data is deleted trough app manager.

晚了,但我希望有所帮助。官方android文档说“当用户连接与您的设备过滤器匹配的设备时,系统会向他们显示一个对话框,询问他们是否要启动您的应用程序。如果用户接受,您的应用程序将自动获得访问设备的权限,直到设备被断开连接。”。如果您还选中记住设备的复选框,则在应用程序卸载或通过应用程序管理器删除数据之前授予权限。

So you must do:

所以你必须这样做:

  1. Add intent-filter as Wayne says, (and delete all about permissions in your main activity).
  2. Install app and CLOSE IT.
  3. Unplug and plug your usb.
  4. Accept permmission and check box to remember device.
  5. Enjoy.
  1. 正如韦恩所说,添加意图过滤器(并删除主要活动中的所有权限)。
  2. 安装应用程序并关闭它
  3. 拔下并插入您的 USB。
  4. 接受许可和复选框以记住设备。
  5. 享受。