虽然你提出了这个问题已经有一段时间了...但是如果可以帮助到其他人,这里是我的答案。
在最初的问题中,被接受的答案陈述:
因此,如果您有另一个版本的Android,并且此代码不起作用,则必须检查特定版本操作系统的源代码。
因此,您应该直接从android源代码获取所需的文件。您可以下载与您的版本相关的源代码,或者直接从存储库浏览。
您正在搜索的IUsbManager接口通常位于:
/frameworks/base/android-branch-name/core/java/android/hardware/usb。至于Service Manager,则可以在以下位置找到:
/frameworks/base/android-branch-name/core/java/android/os/
我没有发布代码,因为我认为您在2年多后不再寻找它:)
=== 编辑 ===
如要求,这里是代码。我使其适用于版本6.0.0,但我认为函数调用与5.1相同。待验证。
首先,这是您将获得的android项目结构:
![enter image description here](https://istack.dev59.com/Kytai.webp)
在android.harware.usb中创建IUsbManager.java接口:
package android.hardware.usb;
public interface IUsbManager extends android.os.IInterface
{
public static abstract class Stub extends android.os.Binder implements android.hardware.usb.IUsbManager
{
public Stub()
{
throw new RuntimeException( "Stub!" );
}
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);
}
public void getDeviceList( android.os.Bundle devices ) throws android.os.RemoteException;
public android.os.ParcelFileDescriptor openDevice( java.lang.String deviceName ) throws android.os.RemoteException;
public android.hardware.usb.UsbAccessory getCurrentAccessory() throws android.os.RemoteException;
public android.os.ParcelFileDescriptor openAccessory( android.hardware.usb.UsbAccessory accessory ) throws android.os.RemoteException;
public void setDevicePackage(android.hardware.usb.UsbDevice device, java.lang.String packageName, int userId) throws android.os.RemoteException;
public void setAccessoryPackage( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName ) throws android.os.RemoteException;
public boolean hasDevicePermission(android.hardware.usb.UsbDevice device) throws android.os.RemoteException;
public boolean hasAccessoryPermission( android.hardware.usb.UsbAccessory accessory ) throws android.os.RemoteException;
public void requestDevicePermission( android.hardware.usb.UsbDevice device, java.lang.String packageName, android.app.PendingIntent pi ) throws android.os.RemoteException;
public void requestAccessoryPermission( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName, android.app.PendingIntent pi ) throws android.os.RemoteException;
public void grantDevicePermission( android.hardware.usb.UsbDevice device, int uid ) throws android.os.RemoteException;
public void grantAccessoryPermission( android.hardware.usb.UsbAccessory accessory, int uid ) throws android.os.RemoteException;
public boolean hasDefaults( java.lang.String packageName ) throws android.os.RemoteException;
public void clearDefaults( java.lang.String packageName ) throws android.os.RemoteException;
public void setCurrentFunction( java.lang.String function, boolean makeDefault ) throws android.os.RemoteException;
public void setMassStorageBackingFile( java.lang.String path ) throws android.os.RemoteException;
}
接下来,在android.os中创建Java类ServiceManager.java:
package android.os;
import java.util.Map;
public final class ServiceManager
{
public static IBinder getService( String name )
{
throw new RuntimeException( "Stub!" );
}
public static void addService( String name, IBinder service )
{
throw new RuntimeException( "Stub!" );
}
public static IBinder checkService( String name )
{
throw new RuntimeException( "Stub!" );
}
public static String[] listServices() throws RemoteException
{
throw new RuntimeException( "Stub!" );
}
public static void initServiceCache( Map<String, IBinder> cache )
{
throw new RuntimeException( "Stub!" );
}
}
一旦完成上述步骤,不要忘记在AndroidManifest里添加android.permission.MANAGE_USB权限。
然后您可以使用以下函数调用:
private void manageUSBPermissions() {
if ((this.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Log.i(TAG,"This is a system application");
if (getApplicationContext().checkCallingOrSelfPermission("android.permission.MANAGE_USB") == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG,"I have android.permission.MANAGE_USB");
grantUsbPermissions();
} else {
Log.i(TAG,"I do not have android.permission.MANAGE_USB");
}
} else {
Log.i(TAG,"This is not a system application");
}
}
private void grantUsbPermissions() {
try {
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo( "com.your.package", 0 );
if( ai != null ) {
UsbManager manager = (UsbManager) 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() == 0x1234 ) {
service.grantDevicePermission( device, ai.uid );
service.setDevicePackage( device, "com.your.package", ai.uid );
}
}
}
}
catch ( Exception e ) {
Log.e(TAG, "Error granting USB permissions: " + e);
}
}
检查您的应用程序是否为系统应用程序以及是否拥有正确的权限,否则它将无法正常工作。
还要注意,您的供应商ID不是十六进制的,而是十进制的。