广播接收器未收到USB许可 [英] USB Permissions not received by Broadcast receiver

查看:73
本文介绍了广播接收器未收到USB许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚安社区,我满怀希望接受有关以下问题的教育。

Good evening community, I am reaching with the hopes of being educated about the following problem.

此代码的目的是能够处理USB许可意图在清单文件中注册的接收器中。当用户接受或拒绝提示时,接收方将获得USB附加和分离操作,但没有USB权限。

My intention with this code is to be able to handle USB permission intents in a receiver registered in a manifest file. The receiver gets USB Attached and detached actions, but not USB permissions when the user either accepts or declines the prompt.

以下是清单,接收方和活动的代码将权限请求发送到USB管理器。最后,我的目标SDK是28。

Here is the code for the manifest, receiver and an activity to send the permissions request to the USB manager. And Finally, my target SDK is 28.

我们非常感谢您的帮助。

Any help is very much appreciated. Thank you very much.

public class BroadcastReceiver extends android.content.BroadcastReceiver{

    public static final String USB_DEVICE_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
    public static final String USB_DEVICE_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
    public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";

    @Override
    public void onReceive(Context context, Intent intent) {

        Context applicationContext = context.getApplicationContext();

        try{

            if (intent != null) {
                String action = intent.getAction();

                if (!TextUtils.isEmpty(action)) {

                    if (action.equals(USB_DEVICE_ATTACHED) || action.equals(USB_PERMISSION)){

                        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        UsbManager usbManager = (UsbManager) applicationContext.getSystemService(Context.USB_SERVICE);

                        if (action.equals(USB_DEVICE_ATTACHED)){

                            if (!usbManager.hasPermission(device)){
                                intent.setAction(USB_PERMISSION);
                                intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
                                intent.setClass(applicationContext, PermissionActivity.class);
                                applicationContext.startActivity(intent);
                                Toast.makeText(applicationContext, "Device Attached.", Toast.LENGTH_LONG).show();
                            }
                            else{
                                Toast.makeText(applicationContext, "Permissions already assigned", Toast.LENGTH_LONG).show();
                            }
                        }
                        else if (action.equals(USB_PERMISSION)){

                            if (usbManager.hasPermission(device)){
                                Toast.makeText(applicationContext, "USB Permissions are granted.", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                    else if (action.equals(USB_DEVICE_DETACHED)) {
                        Toast.makeText(applicationContext, "Device Detached.", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        catch(Exception e){
            Toast.makeText(applicationContext, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

以下是活动:

    public class PermissionActivity extends android.support.v7.app.AppCompatActivity {

    public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Context applicationContext = this.getApplicationContext();

        Intent intent = getIntent();
        if (intent != null )
        {
            if (intent.getAction().equals(USB_PERMISSION)){
                if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false )) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (device != null) {
                        UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
                        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(applicationContext, 0, new Intent(USB_PERMISSION), 0);
                        mUsbManager.requestPermission(device, mPermissionIntent);

                        Toast.makeText(applicationContext, "Requesting Permission", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        finish();
    }

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

最后是清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.usbtest">

    <uses-feature android:name="android.hardware.usb.host"/>

    <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"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".PermissionActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"
            android:excludeFromRecents="true"
            android:exported="true"
            android:noHistory="true"
            android:process=":UsbEventReceiverActivityProcess"
            android:taskAffinity="com.example.taskAffinityUsbEventReceiver"
            android:theme="@style/Theme.AppCompat.Translucent">

            <intent-filter>
                <action android:name="com.android.example.USB_PERMISSION"/>
            </intent-filter>
        </activity>

        <receiver android:name=".BroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
                <action android:name="com.android.example.USB_PERMISSION"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>


推荐答案

我发现了问题。自Android 8.0起,清单声明的
广播接收器和可以接收的操作类型受到更多限制。 USB权限操作不是可以接收的受限操作列表的一部分。以下是有关此问题的一些链接。

I found the problem. Since Android 8.0, there are more restrictions with manifest-declared broadcast receivers and the type of actions that can be received. The USB Permissions action is not part of the limited list of actions that can be received. Here are some links regarding this issue.

https://developer.android.com/guide/components/broadcasts#context-registered-recievers
https://developer.android.com/guide/components/broadcast-exceptions

这篇关于广播接收器未收到USB许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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