android app在系统重启绕过USB授权对话框,自动获取USB权限

时间:2025-01-27 09:35:23

最近由于项目需要参考了很多关于不修改android源码情况下实现屏蔽USB授权对话框的博文,结合自身实践阐述给出细节的实现过程。

当前实现的策略是开发一个android内部服务,用于响应需要使用USB授权的app的申请,app需要在使用USB之前主动申请我们自行开发的授权,走在默认授权对话框触发之前提出申请。

一、创建一个android内部服务apk

1)具体使用的

<manifest xmlns:andro
    package="">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver android:name=".LaunchReceiver">
            <intent-filter>
                <action android:name="ACTION_USB_PERMISSION_ISSUER" />
            </intent-filter>
        </receiver>
        <service android:enabled="true" android:name=".StubService" />
    </application>

    <uses-permission android:name=".MANAGE_USB" />
    <uses-permission android:name=".RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name=".READ_EXTERNAL_STORAGE" />

</manifest>

2)建立.替换系统源码

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: frameworks/base/core/java/android/hardware/usb/
 */
package ;
/** @hide */
public interface IUsbManager extends 
{
    /** Local-side IPC implementation stub class. */
    public static abstract class Stub extends  implements 
    {
        private static final  DESCRIPTOR = "";
        /** Construct the stub at attach it to the interface. */
        public Stub()
        {
            (this, DESCRIPTOR);
        }
        /**
         * Cast an IBinder object into an  interface,
         * generating a proxy if needed.
         */
        public static  asInterface( obj)
        {
            if ((obj==null)) {
                return null;
            }
             iin = (DESCRIPTOR);
            if (((iin!=null)&&(iin instanceof ))) {
                return (()iin);
            }
            return new (obj);
        }
        @Override public  asBinder()
        {
            return this;
        }
        @Override public boolean onTransact(int code,  data,  reply, int flags) throws 
        {
            switch (code)
            {
                case INTERFACE_TRANSACTION:
                {
                    (DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_getDeviceList:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = new ();
                    (_arg0);
                    ();
                    if ((_arg0!=null)) {
                        (1);
                        _arg0.writeToParcel(reply, .PARCELABLE_WRITE_RETURN_VALUE);
                    }
                    else {
                        (0);
                    }
                    return true;
                }
                case TRANSACTION_openDevice:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = ();
                     _result = (_arg0);
                    ();
                    if ((_result!=null)) {
                        (1);
                        _result.writeToParcel(reply, .PARCELABLE_WRITE_RETURN_VALUE);
                    }
                    else {
                        (0);
                    }
                    return true;
                }
                case TRANSACTION_getCurrentAccessory:
                {
                    (DESCRIPTOR);
                     _result = ();
                    ();
                    if ((_result!=null)) {
                        (1);
                        _result.writeToParcel(reply, .PARCELABLE_WRITE_RETURN_VALUE);
                    }
                    else {
                        (0);
                    }
                    return true;
                }
                case TRANSACTION_openAccessory:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                     _result = (_arg0);
                    ();
                    if ((_result!=null)) {
                        (1);
                        _result.writeToParcel(reply, .PARCELABLE_WRITE_RETURN_VALUE);
                    }
                    else {
                        (0);
                    }
                    return true;
                }
                case TRANSACTION_setDevicePackage:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                     _arg1;
                    _arg1 = ();
                    int _arg2;
                    _arg2 = ();
                    (_arg0, _arg1, _arg2);
                    ();
                    return true;
                }
                case TRANSACTION_setAccessoryPackage:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                     _arg1;
                    _arg1 = ();
                    int _arg2;
                    _arg2 = ();
                    (_arg0, _arg1, _arg2);
                    ();
                    return true;
                }
                case TRANSACTION_hasDevicePermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                    boolean _result = (_arg0);
                    ();
                    (((_result)?(1):(0)));
                    return true;
                }
                case TRANSACTION_hasAccessoryPermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                    boolean _result = (_arg0);
                    ();
                    (((_result)?(1):(0)));
                    return true;
                }
                case TRANSACTION_requestDevicePermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                     _arg1;
                    _arg1 = ();
                     _arg2;
                    if ((0!=())) {
                        _arg2 = (data);
                    }
                    else {
                        _arg2 = null;
                    }
                    (_arg0, _arg1, _arg2);
                    ();
                    return true;
                }
                case TRANSACTION_requestAccessoryPermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                     _arg1;
                    _arg1 = ();
                     _arg2;
                    if ((0!=())) {
                        _arg2 = (data);
                    }
                    else {
                        _arg2 = null;
                    }
                    (_arg0, _arg1, _arg2);
                    ();
                    return true;
                }
                case TRANSACTION_grantDevicePermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                    int _arg1;
                    _arg1 = ();
                    (_arg0, _arg1);
                    ();
                    return true;
                }
                case TRANSACTION_grantAccessoryPermission:
                {
                    (DESCRIPTOR);
                     _arg0;
                    if ((0!=())) {
                        _arg0 = (data);
                    }
                    else {
                        _arg0 = null;
                    }
                    int _arg1;
                    _arg1 = ();
                    (_arg0, _arg1);
                    ();
                    return true;
                }
                case TRANSACTION_hasDefaults:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = ();
                    int _arg1;
                    _arg1 = ();
                    boolean _result = (_arg0, _arg1);
                    ();
                    (((_result)?(1):(0)));
                    return true;
                }
                case TRANSACTION_clearDefaults:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = ();
                    int _arg1;
                    _arg1 = ();
                    (_arg0, _arg1);
                    ();
                    return true;
                }
                case TRANSACTION_setCurrentFunction:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = ();
                    boolean _arg1;
                    _arg1 = (0!=());
                    (_arg0, _arg1);
                    ();
                    return true;
                }
                case TRANSACTION_setMassStorageBackingFile:
                {
                    (DESCRIPTOR);
                     _arg0;
                    _arg0 = ();
                    (_arg0);
                    ();
                    return true;
                }
                case TRANSACTION_allowUsbDebugging:
                {
                    (DESCRIPTOR);
                    boolean _arg0;
                    _arg0 = (0!=());
                     _arg1;
                    _arg1 = ();
                    (_arg0, _arg1);
                    ();
                    return true;
                }
                case TRANSACTION_denyUsbDebugging:
                {
                    (DESCRIPTOR);
                    ();
                    ();
                    return true;
                }
                case TRANSACTION_clearUsbDebuggingKeys:
                {
                    (DESCRIPTOR);
                    ();
                    ();
                    return true;
                }
            }
            return (code, data, reply, flags);
        }
        private static class Proxy implements 
        {
            private  mRemote;
            Proxy( remote)
            {
                mRemote = remote;
            }
            @Override public  asBinder()
            {
                return mRemote;
            }
            public  getInterfaceDescriptor()
            {
                return DESCRIPTOR;
            }
            /* Returns a list of all currently attached USB devices */
            @Override public void getDeviceList( devices) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    (Stub.TRANSACTION_getDeviceList, _data, _reply, 0);
                    _reply.readException();
                    if ((0!=_reply.readInt())) {
                        (_reply);
                    }
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Returns a file descriptor for communicating with the USB device.
                 * The native fd can be passed to usb_device_new() in libusbhost.
                 */
            @Override public  openDevice( deviceName) throws 
            {
                 _data = ();
                 _reply = ();
                 _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(deviceName);
                    (Stub.TRANSACTION_openDevice, _data, _reply, 0);
                    _reply.readException();
                    if ((0!=_reply.readInt())) {
                        _result = (_reply);
                    }
                    else {
                        _result = null;
                    }
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* Returns the currently attached USB accessory */
            @Override public  getCurrentAccessory() throws 
            {
                 _data = ();
                 _reply = ();
                 _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    (Stub.TRANSACTION_getCurrentAccessory, _data, _reply, 0);
                    _reply.readException();
                    if ((0!=_reply.readInt())) {
                        _result = (_reply);
                    }
                    else {
                        _result = null;
                    }
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* Returns a file descriptor for communicating with the USB accessory.
                 * This file descriptor can be used with standard Java file operations.
                 */
            @Override public  openAccessory( accessory) throws 
            {
                 _data = ();
                 _reply = ();
                 _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((accessory!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    (Stub.TRANSACTION_openAccessory, _data, _reply, 0);
                    _reply.readException();
                    if ((0!=_reply.readInt())) {
                        _result = (_reply);
                    }
                    else {
                        _result = null;
                    }
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* Sets the default package for a USB device
                 * (or clears it if the package name is null)
                 */
            @Override public void setDevicePackage( device,  packageName, int userId) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((device!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeString(packageName);
                    _data.writeInt(userId);
                    (Stub.TRANSACTION_setDevicePackage, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Sets the default package for a USB accessory
                 * (or clears it if the package name is null)
                 */
            @Override public void setAccessoryPackage( accessory,  packageName, int userId) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((accessory!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeString(packageName);
                    _data.writeInt(userId);
                    (Stub.TRANSACTION_setAccessoryPackage, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Returns true if the caller has permission to access the device. */
            @Override public boolean hasDevicePermission( device) throws 
            {
                 _data = ();
                 _reply = ();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((device!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    (Stub.TRANSACTION_hasDevicePermission, _data, _reply, 0);
                    _reply.readException();
                    _result = (0!=_reply.readInt());
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* Returns true if the caller has permission to access the accessory. */
            @Override public boolean hasAccessoryPermission( accessory) throws 
            {
                 _data = ();
                 _reply = ();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((accessory!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    (Stub.TRANSACTION_hasAccessoryPermission, _data, _reply, 0);
                    _reply.readException();
                    _result = (0!=_reply.readInt());
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* 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.
                 */
            @Override public void requestDevicePermission( device,  packageName,  pi) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((device!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeString(packageName);
                    if ((pi!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    (Stub.TRANSACTION_requestDevicePermission, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* 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.
                 */
            @Override public void requestAccessoryPermission( accessory,  packageName,  pi) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((accessory!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeString(packageName);
                    if ((pi!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    (Stub.TRANSACTION_requestAccessoryPermission, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Grants permission for the given UID to access the device */
            @Override public void grantDevicePermission( device, int uid) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((device!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeInt(uid);
                    (Stub.TRANSACTION_grantDevicePermission, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Grants permission for the given UID to access the accessory */
            @Override public void grantAccessoryPermission( accessory, int uid) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((accessory!=null)) {
                        _data.writeInt(1);
                        (_data, 0);
                    }
                    else {
                        _data.writeInt(0);
                    }
                    _data.writeInt(uid);
                    (Stub.TRANSACTION_grantAccessoryPermission, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Returns true if the USB manager has default preferences or permissions for the package */
            @Override public boolean hasDefaults( packageName, int userId) throws 
            {
                 _data = ();
                 _reply = ();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(packageName);
                    _data.writeInt(userId);
                    (Stub.TRANSACTION_hasDefaults, _data, _reply, 0);
                    _reply.readException();
                    _result = (0!=_reply.readInt());
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
            /* Clears default preferences and permissions for the package */
            @Override public void clearDefaults( packageName, int userId) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(packageName);
                    _data.writeInt(userId);
                    (Stub.TRANSACTION_clearDefaults, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Sets the current USB function. */
            @Override public void setCurrentFunction( function, boolean makeDefault) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(function);
                    _data.writeInt(((makeDefault)?(1):(0)));
                    (Stub.TRANSACTION_setCurrentFunction, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Sets the file path for USB mass storage backing file. */
            @Override public void setMassStorageBackingFile( path) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(path);
                    (Stub.TRANSACTION_setMassStorageBackingFile, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Allow USB debugging from the attached host. If alwaysAllow is true, add the
                 * the public key to list of host keys that the user has approved.
                 */
            @Override public void allowUsbDebugging(boolean alwaysAllow,  publicKey) throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeInt(((alwaysAllow)?(1):(0)));
                    _data.writeString(publicKey);
                    (Stub.TRANSACTION_allowUsbDebugging, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Deny USB debugging from the attached host */
            @Override public void denyUsbDebugging() throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    (Stub.TRANSACTION_denyUsbDebugging, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
            /* Clear public keys installed for secure USB debugging */
            @Override public void clearUsbDebuggingKeys() throws 
            {
                 _data = ();
                 _reply = ();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    (Stub.TRANSACTION_clearUsbDebuggingKeys, _data, _reply, 0);
                    _reply.readException();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
        }
        static final int TRANSACTION_getDeviceList = (.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_openDevice = (.FIRST_CALL_TRANSACTION + 1);
        static final int TRANSACTION_getCurrentAccessory = (.FIRST_CALL_TRANSACTION + 2);
        static final int TRANSACTION_openAccessory = (.FIRST_CALL_TRANSACTION + 3);
        static final int TRANSACTION_setDevicePackage = (.FIRST_CALL_TRANSACTION + 4);
        static final int TRANSACTION_setAccessoryPackage = (.FIRST_CALL_TRANSACTION + 5);
        static final int TRANSACTION_hasDevicePermission = (.FIRST_CALL_TRANSACTION + 6);
        static final int TRANSACTION_hasAccessoryPermission = (.FIRST_CALL_TRANSACTION + 7);
        static final int TRANSACTION_requestDevicePermission = (.FIRST_CALL_TRANSACTION + 8);
        static final int TRANSACTION_requestAccessoryPermission = (.FIRST_CALL_TRANSACTION + 9);
        static final int TRANSACTION_grantDevicePermission = (.FIRST_CALL_TRANSACTION + 10);
        static final int TRANSACTION_grantAccessoryPermission = (.FIRST_CALL_TRANSACTION + 11);
        static final int TRANSACTION_hasDefaults = (.FIRST_CALL_TRANSACTION + 12);
        static final int TRANSACTION_clearDefaults = (.FIRST_CALL_TRANSACTION + 13);
        static final int TRANSACTION_setCurrentFunction = (.FIRST_CALL_TRANSACTION + 14);
        static final int TRANSACTION_setMassStorageBackingFile = (.FIRST_CALL_TRANSACTION + 15);
        static final int TRANSACTION_allowUsbDebugging = (.FIRST_CALL_TRANSACTION + 16);
        static final int TRANSACTION_denyUsbDebugging = (.FIRST_CALL_TRANSACTION + 17);
        static final int TRANSACTION_clearUsbDebuggingKeys = (.FIRST_CALL_TRANSACTION + 18);
    }
    /* Returns a list of all currently attached USB devices */
    public void getDeviceList( devices) throws ;
    /* Returns a file descriptor for communicating with the USB device.
         * The native fd can be passed to usb_device_new() in libusbhost.
         */
    public  openDevice( deviceName) throws ;
    /* Returns the currently attached USB accessory */
    public  getCurrentAccessory() throws ;
    /* Returns a file descriptor for communicating with the USB accessory.
         * This file descriptor can be used with standard Java file operations.
         */
    public  openAccessory( accessory) throws ;
    /* Sets the default package for a USB device
         * (or clears it if the package name is null)
         */
    public void setDevicePackage( device,  packageName, int userId) throws ;
    /* Sets the default package for a USB accessory
         * (or clears it if the package name is null)
         */
    public void setAccessoryPackage( accessory,  packageName, int userId) throws ;
    /* Returns true if the caller has permission to access the device. */
    public boolean hasDevicePermission( device) throws ;
    /* Returns true if the caller has permission to access the accessory. */
    public boolean hasAccessoryPermission( accessory) throws ;
    /* 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( device,  packageName,  pi) throws ;
    /* 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( accessory,  packageName,  pi) throws ;
    /* Grants permission for the given UID to access the device */
    public void grantDevicePermission( device, int uid) throws ;
    /* Grants permission for the given UID to access the accessory */
    public void grantAccessoryPermission( accessory, int uid) throws ;
    /* Returns true if the USB manager has default preferences or permissions for the package */
    public boolean hasDefaults( packageName, int userId) throws ;
    /* Clears default preferences and permissions for the package */
    public void clearDefaults( packageName, int userId) throws ;
    /* Sets the current USB function. */
    public void setCurrentFunction( function, boolean makeDefault) throws ;
    /* Sets the file path for USB mass storage backing file. */
    public void setMassStorageBackingFile( path) throws ;
    /* Allow USB debugging from the attached host. If alwaysAllow is true, add the
         * the public key to list of host keys that the user has approved.
         */
    public void allowUsbDebugging(boolean alwaysAllow,  publicKey) throws ;
    /* Deny USB debugging from the attached host */
    public void denyUsbDebugging() throws ;
    /* Clear public keys installed for secure USB debugging */
    public void clearUsbDebuggingKeys() throws ;
} 

3)建立替换源码

package ;

import ;

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!" );
    }
}

4)建立响应app申请的BroadcastReceiver类,

package ;

import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import .;

import ;

public class LaunchReceiver extends BroadcastReceiver
{
    private final String TAG = "";

    private final String ACTION_USB_PERMISSION_ISSUER = "ACTION_USB_PERMISSION_ISSUER";

    private LogManager logger;

    public void onReceive( Context context, Intent intent )
    {
        logger = new LogManager(context);

        String action = ();
        if( action != null && ( ACTION_USB_PERMISSION_ISSUER ) )
        {
            ("Receiver event processing.");
            try {
                UsbDeviceDescriptor deviceFilter = new UsbDeviceDescriptor();
                 = ("packageName");
                 = ("vendorId", -1);
                 = ("productId", -1);
                 = ("deviceClass", -1);
                 = ("deviceSubclass", -1);

                if(()){
                    ("PackageName is Null");
                    return;
                }

                PackageManager pm = ();
                ApplicationInfo ai = (, 0);

                UsbManager manager = (UsbManager) (Context.USB_SERVICE);
                IBinder b = (Context.USB_SERVICE);
                IUsbManager service = (b);
                HashMap<String, UsbDevice> deviceList = ();

                ("List of usb devices was loaded. Number of attached devices: " +
                        new Integer(()).toString());

                if(( != -1 &&  != -1) ||
                        ( != -1 &&  != -1)) {

                    ("The usb permission will be granted for application " + );

                    Iterator<UsbDevice> deviceIterator = ().iterator();
                    while (()) {
                        UsbDevice device = ();

                        if ((() ==  &&
                                () == ) ||
                                (() ==  &&
                                        () == )) {

                            try {
                                (device, );
                                (device, , );

                                ("Usb permission is granted for application " +
                                         + " for device " + () +
                                        " and user " +  + " is granted");
                            } catch (SecurityException se) {
                                (());
                                (TAG, ());
                            }
                        }

                    }
                } else {
                    ("Device params is Error "+());
                }
            }
            catch(Exception e)
            {
                (());
                (TAG, ());
            }
        }
    }
}

额外需要的和一并给出,日志实现自行替换

package ;

//二选一,确定USB设备
public class UsbDeviceDescriptor {

    public String packageName;

    //第一组,确定USB设备
    public int deviceClass;
    public int deviceSubclass;
    //第二级,确定USB设备
    public int vendorId;
    public int productId;

    @Override
    public String toString() {
        return "packageName:" + packageName
                + ", deviceClass:" + deviceClass
                + ", deviceSubclass: " + deviceSubclass
                + ", vendorId: " + vendorId
                + ", productId: " + productId;
    }

}
package ;

import ;
import ;
import ;
import ;
import ;


public class StubService extends Service
{
    private static final String TAG = "StubService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        (TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        (TAG, "onStart");
    }
}

5)以上关键文件工程化后生成,需要将其拷贝到android系统下的/system/prv-app目录下(该目录是系统预装apk路径),并修改其文件权限,例如777

备注:我的做法是直接下载了“kingroot”和“RE文件管理”,安装后,使用“kingroot”给“RE文件管理”开启root权限,然后通过“RE文件管理”将“”拷贝到/system/prv-app目录下,并修改其文件权限(选择文件长按,RE文件管理会出现更多功能菜单,里面有权限修改)

6)在需要的app进行动态广播申请,实例如下:

    private void UsbInit()
    {
        UsbInitPermission(1);
        try {
        (100);
        } catch (InterruptedException e) {
        ();
        }
        UsbInitPermission(2);
        try {
        (100);
        } catch (InterruptedException e) {
        ();
        }
        UsbInitPermission(3);
        try {
        (100);
        } catch (InterruptedException e) {
        ();
        }
        UsbInitPermission(4);
        try {
        (100);
        } catch (InterruptedException e) {
        ();
        }
    }

    static final String YOUR_APP_PACKAGE_NAMESPACE = "你的app-package";
    static final String ACTION_USB_PERMISSION_APP =  "ACTION_USB_PERMISSION_ISSUER";
    private void UsbInitPermission(int id)
    {
        Intent intent_usb = new Intent();
        intent_usb.setAction(ACTION_USB_PERMISSION_APP);
        intent_usb.putExtra("packageName", YOUR_APP_PACKAGE_NAMESPACE);
         //device_filter.xml里的字段
        intent_usb.putExtra("vendorId", 1659);
        switch(id)
        {
        case 1:
        intent_usb.putExtra("productId", 8963);
        break;
        case 2:
        intent_usb.putExtra("productId", 9553);
        break;
        case 3:
        intent_usb.putExtra("productId", 8964);
        break;
        case 4:
        intent_usb.putExtra("productId", 9475);
        break;
        }
        //发送广播
        sendBroadcast(intent_usb);
    }-package";
    static final String ACTION_USB_PERMISSION_APP =  "ACTION_USB_PERMISSION_ISSUER";
    private void UsbInitPermission(int id)
    {
        Intent intent_usb = new Intent();
        intent_usb.setAction(ACTION_USB_PERMISSION_APP);
        intent_usb.putExtra("packageName", YOUR_APP_PACKAGE_NAMESPACE);
         //device_filter.xml里的字段
        intent_usb.putExtra("vendorId", 1659);
        switch(id)
        {
        case 1:
        intent_usb.putExtra("productId", 8963);
        break;
        case 2:
        intent_usb.putExtra("productId", 9553);
        break;
        case 3:
        intent_usb.putExtra("productId", 8964);
        break;
        case 4:
        intent_usb.putExtra("productId", 9475);
        break;
        }
        //发送广播
        sendBroadcast(intent_usb);
    }

然后再合适的地方调用UsbInit(),等待一段时间后会获得授权,再使用你的USB,这样就不会再有USB授权对话框弹出

7)如果懒得编译授权的apk,可以直接采用/download/py8105/10313534,再完成5、6步骤。