Android的USB自动授予权限 [英] Android USB Automatically Grant Permission
问题描述
反正是有访问IUsbManager接口类调用service.grantDevicePermission()方法来自动设置为USB设备的许可,而不总是具有ConfirmationActivity显示询问用户?
Is there anyway to access the IUsbManager Interface class to call the service.grantDevicePermission() method to automatically set the permission for the USB device without always having the ConfirmationActivity show up asking the user?
感谢
推荐答案
当然是可以!你可以使用这个code,为我工作:
Of course yes! you can use this code, worked for me:
public boolean grantAutomaticPermission(UsbDevice usbDevice)
{
try
{
Context context=YourActivityOrApplication;
PackageManager pkgManager=context.getPackageManager();
ApplicationInfo appInfo=pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Class serviceManagerClass=Class.forName("android.os.ServiceManager");
Method getServiceMethod=serviceManagerClass.getDeclaredMethod("getService",String.class);
getServiceMethod.setAccessible(true);
android.os.IBinder binder=(android.os.IBinder)getServiceMethod.invoke(null, Context.USB_SERVICE);
Class iUsbManagerClass=Class.forName("android.hardware.usb.IUsbManager");
Class stubClass=Class.forName("android.hardware.usb.IUsbManager$Stub");
Method asInterfaceMethod=stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
asInterfaceMethod.setAccessible(true);
Object iUsbManager=asInterfaceMethod.invoke(null, binder);
System.out.println("UID : " + appInfo.uid + " " + appInfo.processName + " " + appInfo.permission);
final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class,int.class);
grantDevicePermissionMethod.setAccessible(true);
grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
System.out.println("Method OK : " + binder + " " + iUsbManager);
return true;
}
catch(Exception e)
{
System.err.println("Error trying to assing automatic usb permission : ");
e.printStackTrace();
return false;
}
}
我所做的是实现机器人来完成这个使用反射。为了使用这个code,你必须有:
What i did is implement the way android does this using reflection. In order to use this code you must have the:
<uses-permission android:name="android.permission.MANAGE_USB"/>
但是有一个问题,我不确切知道,纠正我,如果我错了:用户持有的权限,而不是应用程序,然后如果你尝试启动从应用程序仪表板应用程序,你就会失败。既要保证比Android开始你aplication,例如,将其设置为启动器,让它在家里的意图开始。
However there is an issue, i don't known exactly, correct me if i am wrong: The USER holds the permission, not the application, then if you try to start the application from applications dash board you will fail. Is necessary to ensure than android begins your aplication, for example, setting it as launcher and let it start in a HOME intent.
问候,希望可以帮助别人。
Greetings, expect can help someone.
这篇关于Android的USB自动授予权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!