Android的USB权限对话框永远不会出现

编程入门 行业动态 更新时间:2024-10-07 13:19:54
本文介绍了Android的USB权限对话框永远不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个简单的应用程序将命令发送到USB打印机通过USB连接到Android 4.0的平板电脑。出于某种原因,我不能让过去获得的权限要求的界面,打开连接。以下是有关code:

I've written a simple app to send commands to a USB printer connected to an Android 4.0 tablet via USB. For some reason, I am unable to get past obtaining permissions to claim an interface and open a connection. Here's the relevant code :

public class TestPrintActivity extends Activity { private UsbManager mUsbManager; private UsbDevice mDevice; private UsbDeviceConnection mConnection; private UsbInterface mInterface; private UsbEndpoint mBulkIn; private UsbEndpoint mBulkOut; private static final String ACTION_USB_PERMISSION = "com.demo.xprinter.USB_PERMISSION"; private PendingIntent mPermissionIntent; private BroadcastReceiver mUsbReceiver; @Override private static final String ACTION_USB_PERMISSION = "com.demo.printerDemo.USB_PERMISSION"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_print); mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); registerReceiver(mUsbReceiver, filter); mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if(device != null){ //call method to set up device communication openPort(device); } } else { Toast.makeText(getApplicationContext(), "Denied!", Toast.LENGTH_SHORT).show(); } } } } }; } public void onResume() { super.onResume(); HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList(); for (HashMap.Entry<String,UsbDevice> entry : deviceList.entrySet()) { UsbDevice aDevice = entry.getValue(); if ((aDevice.getProductId() == 8965) && (aDevice.getVendorId() == 1659) ){ if (mUsbManager.hasPermission(aDevice)) openPort(aDevice); else mUsbManager.requestPermission(aDevice, mPermissionIntent); break; } } }

下面是清单的相关部分:

Here's the relevant part of the manifest:

<manifest xmlns:android="schemas.android/apk/res/android" package="com.demo.printerDemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <uses-feature android:name="android.hardware.usb.host"/>

我已经配置了设备,使其启动我的应用程序,只要打印机连接,并且设备枚举(onResume()),并许可请求被调用过程中发现的。但是,不管是什么原因,我从来没有看到请求权限对话框(我已经看到了它的网上截图)也没有的onReceive()曾经被调用。任何想法,为什么这可能发生?

I've configured the device so that it launches my app whenever the printer is connected, and the device is found during the enumeration (onResume()) and the request for permission is called. However, for whatever reason, I never see the "Request Permission" dialog (I've seen screenshots of it online) nor does onReceive() ever get called. Any ideas why this might be happening?

推荐答案

所以,事实证明,SystemUI.apk / SystemUI.odex在/系统/曾分别被更改为SystemUI.apk.backup / SystemUI.odex.backup。这presumably是prevent的系统UI,从出渣了预期的信息亭模式下,设备的某些方面。在我的previous评论中的logcat项是大线索。一旦文件名进行恢复,我开始看到的权限对话框。

So it turns out that SystemUI.apk/SystemUI.odex in /system/ had been change to SystemUI.apk.backup/SystemUI.odex.backup respectively. This presumably was to prevent some aspects of the System UI from mucking up the intended "kiosk" mode of the device. The logcat entry in my previous comment was the big clue. Once the filenames were restored, I started seeing the Permission dialog.

更多推荐

Android的USB权限对话框永远不会出现

本文发布于:2023-11-28 04:15:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   对话框   权限   Android   USB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!