如何忽略usb host 模式设备连接确认对话框

来源:互联网 发布:淘宝下架眼药水 编辑:程序博客网 时间:2024/06/06 01:12

参考原文地址:http://stackoverflow.com/questions/13726583/bypass-android-usb-host-permision-confirmation-dialog/19681849#19681849

这篇文章提到三种方法可以实现所需要的效果:

1,把您的app放到系统应用文件夹下:/system/app/...

2,在AndroidManifest.xml中添加上系统签名;

以上两种方法作为第三方应用的开发者来说都有点难度,第三种方法的实现除了安装apk应用之后第一次插上USB设备弹出对话框:


勾选上 “默认情况下用于该USB设备”,点击确定即可。每次开机后默认打开该app应用。


使用本文提到的第三种方法确实可以避免每次打开app应用都弹出对话框的请求;

3,步骤一:在你的项目中加入包 android.hardware.usb,添加接口文件IUsbManager.java:

package android.hardware.usb;public interface IUsbManager extends android.os.IInterface{    /** Local-side IPC implementation stub class. */    public static abstract class Stub extends android.os.Binder implements android.hardware.usb.IUsbManager    {        /** Construct the stub at attach it to the interface. */        public Stub()        {            throw new RuntimeException( "Stub!" );        }        /**         * Cast an IBinder object into an android.hardware.usb.IUsbManager interface,         * generating a proxy if needed.         */        public static android.hardware.usb.IUsbManager asInterface( android.os.IBinder obj )        {            throw new RuntimeException( "Stub!" );        }        public android.os.IBinder asBinder()        {            throw new RuntimeException( "Stub!" );        }        public boolean onTransact( int code, android.os.Parcel data, android.os.Parcel reply, int flags ) throws android.os.RemoteException        {            throw new RuntimeException( "Stub!" );        }        static final int TRANSACTION_getDeviceList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);        static final int TRANSACTION_openDevice = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);        static final int TRANSACTION_getCurrentAccessory = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);        static final int TRANSACTION_openAccessory = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);        static final int TRANSACTION_setDevicePackage = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);        static final int TRANSACTION_setAccessoryPackage = (android.os.IBinder.FIRST_CALL_TRANSACTION + 5);        static final int TRANSACTION_hasDevicePermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 6);        static final int TRANSACTION_hasAccessoryPermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 7);        static final int TRANSACTION_requestDevicePermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 8);        static final int TRANSACTION_requestAccessoryPermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 9);        static final int TRANSACTION_grantDevicePermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 10);        static final int TRANSACTION_grantAccessoryPermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 11);        static final int TRANSACTION_hasDefaults = (android.os.IBinder.FIRST_CALL_TRANSACTION + 12);        static final int TRANSACTION_clearDefaults = (android.os.IBinder.FIRST_CALL_TRANSACTION + 13);        static final int TRANSACTION_setCurrentFunction = (android.os.IBinder.FIRST_CALL_TRANSACTION + 14);        static final int TRANSACTION_setMassStorageBackingFile = (android.os.IBinder.FIRST_CALL_TRANSACTION + 15);    }    /* Returns a list of all currently attached USB devices */    public void getDeviceList( android.os.Bundle devices ) throws android.os.RemoteException;    /* Returns a file descriptor for communicating with the USB device.         * The native fd can be passed to usb_device_new() in libusbhost.         */    public android.os.ParcelFileDescriptor openDevice( java.lang.String deviceName ) throws android.os.RemoteException;    /* Returns the currently attached USB accessory */    public android.hardware.usb.UsbAccessory getCurrentAccessory() throws android.os.RemoteException;    /* Returns a file descriptor for communicating with the USB accessory.         * This file descriptor can be used with standard Java file operations.         */    public android.os.ParcelFileDescriptor openAccessory( android.hardware.usb.UsbAccessory accessory ) throws android.os.RemoteException;    /* Sets the default package for a USB device         * (or clears it if the package name is null)         */    public void setDevicePackage( android.hardware.usb.UsbDevice device, java.lang.String packageName ) throws android.os.RemoteException;    /* Sets the default package for a USB accessory         * (or clears it if the package name is null)         */    public void setAccessoryPackage( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName ) throws android.os.RemoteException;    /* Returns true if the caller has permission to access the device. */    public boolean hasDevicePermission(android.hardware.usb.UsbDevice device) throws android.os.RemoteException;    /* Returns true if the caller has permission to access the accessory. */    public boolean hasAccessoryPermission( android.hardware.usb.UsbAccessory accessory ) throws android.os.RemoteException;    /* Requests permission for the given package to access the device.         * Will display a system dialog to query the user if permission         * had not already been given.         */    public void requestDevicePermission( android.hardware.usb.UsbDevice device, java.lang.String packageName, android.app.PendingIntent pi ) throws android.os.RemoteException;    /* Requests permission for the given package to access the accessory.         * Will display a system dialog to query the user if permission         * had not already been given. Result is returned via pi.         */    public void requestAccessoryPermission( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName, android.app.PendingIntent pi ) throws android.os.RemoteException;    /* Grants permission for the given UID to access the device */    public void grantDevicePermission( android.hardware.usb.UsbDevice device, int uid ) throws android.os.RemoteException;    /* Grants permission for the given UID to access the accessory */    public void grantAccessoryPermission( android.hardware.usb.UsbAccessory accessory, int uid ) throws android.os.RemoteException;    /* Returns true if the USB manager has default preferences or permissions for the package */    public boolean hasDefaults( java.lang.String packageName ) throws android.os.RemoteException;    /* Clears default preferences and permissions for the package */    public void clearDefaults( java.lang.String packageName ) throws android.os.RemoteException;    /* Sets the current USB function. */    public void setCurrentFunction( java.lang.String function, boolean makeDefault ) throws android.os.RemoteException;    /* Sets the file path for USB mass storage backing file. */    public void setMassStorageBackingFile( java.lang.String path ) throws android.os.RemoteException;}

步骤二:再创建android.os包,添加ServiceManager.java类:
package android.os;import java.util.Map;public final class ServiceManager{    public static IBinder getService( String name )    {        throw new RuntimeException( "Stub!" );    }    /**     * Place a new @a service called @a name into the service     * manager.     *      * @param name the name of the new service     * @param service the service object     */    public static void addService( String name, IBinder service )    {        throw new RuntimeException( "Stub!" );    }    /**     * Retrieve an existing service called @a name from the     * service manager.  Non-blocking.     */    public static IBinder checkService( String name )    {        throw new RuntimeException( "Stub!" );    }    public static String[] listServices() throws RemoteException    {        throw new RuntimeException( "Stub!" );    }    /**     * This is only intended to be called when the process is first being brought     * up and bound by the activity manager. There is only one thread in the process     * at that time, so no locking is done.     *      * @param cache the cache of service references     * @hide     */    public static void initServiceCache( Map<String, IBinder> cache )    {        throw new RuntimeException( "Stub!" );    }}

步骤三:在AndroidManifest.xml中加入权限
<uses-permission android:name="android.permission.MANAGE_USB" />
这时系统会提示错误:Permission  is only granted to system apps.
解决:Window -> Preferences -> Android -> Lint Error Checking.找到ID=ProtectedPermission这项设置它的”serverity“比"error"低即可。

到这里执行就OK了。





0 0
原创粉丝点击