将android.permission.MANAGE_USB权限添加到现有系统应用程序

时间:2015-04-09 15:00:14

标签: java android

我们在根设备上安装了一个应用程序作为系统应用程序。后者没有android.permission.MANAGE_USB,我们想将它添加到更新的版本,以便自动给予用户权限。我跟着帖子了:
bypass android usb host permission confirmation dialog
更具体地说,答案从用户d_d_t标记为正确。

为什么不起作用?我的意思是,我已经采用了这个更新的版本,直接安装它作为我们的原始系统应用程序,它的工作完美!但是,当我更新现有版本而没有新版本的usb权限时,它就无法正常工作。新的只是一个更新。相同的证书和所有。
我不知所措。有什么想法吗?

我们的代码如下:
的AndroidManifest.xml。

<uses-permission
android:name="android.permission.MANAGE_USB"
android:protectionLevel="signature" />

Java类。

try {
        PackageManager pm = getApplicationContext().getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(getApplicationContext().getPackageName(), 0);
        if (ai != null) {
            UsbManager manager = (UsbManager) getApplicationContext().getSystemService(Context.USB_SERVICE);
            IBinder b = ServiceManager.getService(Context.USB_SERVICE);
            IUsbManager service = IUsbManager.Stub.asInterface(b);

            HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            while (deviceIterator.hasNext()) {
                UsbDevice device = deviceIterator.next();
                if (device.getVendorId() == 0x0403) {
                    service.grantDevicePermission(device, ai.uid);
                    service.setDevicePackage(device,getApplicationContext().getPackageName(), 0);
                }
            }
        }

    } catch (Exception e) {
        Log.e("UsbReceiver", "exception " + e);
        e.printStackTrace();
    }

0 个答案:

没有答案
相关问题