USB设备访问弹出苏pression?弹出、设备、USB、pression

2023-09-12 00:45:02 作者:抑郁成疾

当USB设备连接到Android平板电脑,弹出出现要求用户权限。我想晚饭preSS这是客户不想要它。我应该怎么去的?

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?

在code:

UsbManager.requestpermission(); 

时,称为给USB设备临时访问。

is called to give the USB device temporary access.

这将引发弹出。我如何晚饭preSS的弹出窗口或在默认情况下授予访问权限的用户?

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>

(我有一个复杂的任务/进程布局,我的服务,因人而异在这方面)。

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

和最后一块拼图,透明主题(我不知道,但你很可能使用内置的Andr​​oid的半透明主题) - RES /价值/ 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>