Android的USB权限对话框永远不会出现 [英] Android USB Permissions Dialog never appears

查看:6338
本文介绍了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="http://schemas.android.com/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权限对话框永远不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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