android 如何通过反射多层调用

       public static final String ROOT_PATH = "/storage/emulated";
        public final static String PHONE_STORAGE_PATH = "/storage/emulated/0";
        public final static String SDCARD_STORAGE_PATH = "/storage/sdcard0";
        public final static String USB_STORAGE_PATH = "/storage/usbdisk";
        public final static String STORAGE_ROOT_PATH = "/storage"; 

    private final static String METHOD_GET_VOLUME_LIST = "getVolumeList";

    private final static String METHOD_GET_VOLUME_STATE = "getState";
    private final static String METHOD_IS_REMOVABLE = "isRemovable";
    private final static String METHOD_GET_PATH = "getPathFile";
    private final static String METHOD_GET_UUID = "getUuid";


    private Context mContext;

    private StorageManager mStorageManager;

   //  初始化manager对象 

    mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);

    // 反射调用实例
   private void executeInit() {
        if (mStorageManager != null) {
            Method mGetVolumeListMethod = null;
            Object[] mStorageVolumeList = null;


            try {

               // 获取StorageManager类的 getVolumeList()隐藏方法

                mGetVolumeListMethod = mStorageManager.getClass().getMethod(METHOD_GET_VOLUME_LIST, new Class[0]);

               // 激发调用StorageManager类的 getVolumeList()隐藏方法,并返回 数据列表对象 StorageVolume[],将数据保存给Object[]

                mStorageVolumeList = (Object[]) mGetVolumeListMethod.invoke(mStorageManager, new Object[0]);

          
                if (mStorageVolumeList != null && mStorageVolumeList.length > 0) {
                    int mStorageVolumeCount = mStorageVolumeList.length;


                    for (int i = 0; i < mStorageVolumeCount; i++) {

                         // 通过反射方式,使用数据元素对象获取到类反射的方法getPathFile(),并通过该元素对象激发该方法,返回得到File对象

                        File storagePath = (File) mStorageVolumeList[i].getClass()

                                .getMethod(METHOD_GET_PATH, new Class[0]).invoke(mStorageVolumeList[i], new Object[0]);

                        // 类似以上

                        Boolean isRemovable = (Boolean) mStorageVolumeList[i].getClass()
                                .getMethod(METHOD_IS_REMOVABLE, new Class[0])

                                .invoke(mStorageVolumeList[i], new Object[0]); 

                          // 类似以上

                        String mUuid = (String) mStorageVolumeList[i].getClass()
                                .getMethod(METHOD_GET_UUID, new Class[0]).invoke(mStorageVolumeList[i], new Object[0]);
                        String filepath = storagePath.getAbsolutePath();


                        if (!TextUtils.isEmpty(storagePath.getAbsolutePath())) {
                            String state = (String) mStorageVolumeList[i].getClass()
                                    .getMethod(METHOD_GET_VOLUME_STATE, new Class[0])
                                    .invoke(mStorageVolumeList[i], new Object[0]);
                            if (state != null && MOUNTED.equals(state)) {
                                if (!isRemovable) {
                                    LogUtils.i(TAG, "mPhoneStorage is mounted storagePath:" + storagePath);
                                    mPhoneStorage.init(storagePath);
                                    mPhoneStorage.setMountState(true);
                                } else if (mUuid != null && filepath.equals(STORAGE_ROOT_PATH + "/" + mUuid)
                                        || SDCARD_STORAGE_PATH.equals(filepath)) {
                                    LogUtils.i(TAG, "mSDCardStorage is mounted storagePath:" + storagePath);
                                    mSDCardStorage.init(storagePath);
                                    mSDCardStorage.setMountState(true);
                                } else if (USB_STORAGE_PATH.equals(filepath)) {
                                    //mUSBStorageInfo = new StorageUtilImpl.StorageInfo(storagePath);
                                    //mUSBStorageInfo.setMountState(true);
                                }
                            }
                        }
                    }
                }
            } catch (NoSuchMethodException ex) {
                LogUtils.e("executeInit NoSuchMethodException " + ex.toString());
            } catch (IllegalArgumentException ex) {
                LogUtils.e("executeInit IllegalArgumentException  " + ex.toString());
            } catch (IllegalAccessException ex) {
                LogUtils.e("executeInit IllegalAccessException  " + ex.toString());
            } catch (InvocationTargetException ex) {
                LogUtils.e("executeInit InvocationTargetException  " + ex.toString());
            }
        }

    }


//

public static Class EnvironmentEx;
private static Method getExternalStorageMethod;
// 通过反射调用类 EnvironmentEx的静态方法

public static File getExternalStorage(){
    try {
        if(getExternalStorageMethod==null){
            getExternalStorageMethod = EnvironmentEx.getMethod("getExternalStoragePath");
        }
        return ((File)getExternalStorageMethod.invoke(null));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new File("/storage/sdcard0");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值