USB设备访问弹出抑制? [英] USB device access pop-up suppression?

查看:92
本文介绍了USB设备访问弹出抑制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将USB设备连接到Android平板电脑时,会出现一个弹出窗口,要求用户许可.我想抑制这种情况,因为客户端不希望这样做.我该怎么办?

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?

在代码中:

UsbManager.requestpermission(); 

调用

授予USB设备临时访问权限.

is called to give the USB device temporary access.

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

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

推荐答案

当您在应用内请求权限时,默认用于此USB设备"复选框似乎没有任何作用(我不确定为什么该复选框甚至显示在这个弹出窗口上.

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.

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

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>

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

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

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

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

此处的供应商ID和产品ID必须以十进制形式给出-VID和PID的上方均为0x6666.

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

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

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

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

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

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

编辑

总结-如果您针对活动注册了意图过滤器,则在连接USB设备/附件后将立即显示USB许可窗口.如果用户选中默认情况下对此USB设备使用"复选框,然后授予权限,则该权限将被记住并且权限对话框将不会再次显示(除非卸载了应用程序或用户从应用程序管理器中清除了默认操作) ).

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

我在这里放置了一个很小的,可怕的,可行的示例项目:

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

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

您将需要编辑res/xml/usb_device_filter.xml,但否则,这将使您能够非常快速地对其进行测试.

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

对于服务...

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

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.

我在清单中这样定义它:

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>

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

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

我这样定义活动:

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();
    }
}

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

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:

<?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>  

这篇关于USB设备访问弹出抑制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆