android关于反射的使用

初次使用反射时经常会抛异常,可能的原因有以下:

1.该类可能由于版本的原因不存在某个方法了所以获取不到,我们可以通过循环遍历类的所有方法来排查是不是此原因引起的,如果方法不存在就要使用其他的方法替代

2.传入的参数不正确导致没有正确反射到对应的方法,建议根据方法的参数和返回值正确书写代码,以下是一些反射获取方法的例子

java 复制代码
public class Utils {
    private static final String TAG = Utils.class.getSimpleName();
    public static String getSystemProperty(String property, String defaultValue) {
        try {
            Class<?> clazz = Class.forName("android.os.SystemProperties");//获取类
            Method M[] = clazz.getMethods();
            //循环调试获取该类的所有方法
            for (Method m : M) {
                Log.i(TAG, "getSystemProperty: " + m.getName());
            }
            //如果存在该方法,再通过反射获取方法
            Method getter = clazz.getDeclaredMethod("get", String.class, String.class);//获取类的方法,参数依次为(方法名,参数类型,返回值类型)
            String value = (String) getter.invoke(null, property, defaultValue);//调用方法
            if (!TextUtils.isEmpty(value)) {
                return value;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read system properties");
        }
        return defaultValue;
    }

    public static Boolean getSystemPropertyBoolean(String property, Boolean defaultValue) {
        try {
            Class<?> clazz = Class.forName("android.os.SystemProperties");
            Method getter = clazz.getDeclaredMethod("getBoolean", String.class, Boolean.TYPE);//获取类的方法,参数依次为(方法名,参数类型,返回值类型)
            Boolean value = (Boolean) getter.invoke(null, new Object[] { property, defaultValue });
            if (!TextUtils.isEmpty(String.valueOf(value))) {
                return value;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read system properties boolean");
        }
        return defaultValue;
    }

    public static String getStorageVolumePath(StorageVolume storageVolume) {
        try {
            Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            //通过反射调用系统hide的方法
            Method getPath = storageVolumeClazz.getMethod("getPath");
            //通过反射调用getPath
            Log.i(TAG, "getStorageVolumePath: " + getPath.invoke(storageVolume).toString());
            return getPath.invoke(storageVolume).toString(); //获取路径
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read Storage Volume Path");
        }
        return "";
    }

    public static Long getStorageVolumeMaxFileSize(StorageVolume storageVolume) {
        try {
            Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            //通过反射调用系统hide的方法
            Method getMaxFileSize = storageVolumeClazz.getMethod("getMaxFileSize");
            //通过反射调用getPath
            Log.i(TAG, "getStorageVolumeMaxFileSize: " + getMaxFileSize.invoke(storageVolume));
            return (Long) getMaxFileSize.invoke(storageVolume); //获取路径
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read Storage Volume MaxFileSize");
        }
        return 0L;
    }

    public static String getStorageVolumeState(StorageManager storageManager, String path) {
        String result = "";
        try {
            Method getVolumeState = StorageManager.class.getDeclaredMethod("getVolumeState", String.class);
            getVolumeState.setAccessible(true);
            Log.i(TAG, "getStorageVolumeState: "+ getVolumeState.invoke(storageManager, path));
            result = (String) getVolumeState.invoke(storageManager, path);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read method getVolumeState");
        }
        return result;
    }

    public static String getMediaFileMimeTypeForFile(String fileName) {
        try {
            Class<?> mediaFileClazz = Class.forName("android.media.MediaFile");
            //通过反射调用系统hide的方法
            Method getMimeTypeForFile = mediaFileClazz.getMethod("getMimeTypeForFile");
            //通过反射调用getPath
            return (String) getMimeTypeForFile.invoke(fileName); //获取路径
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            Log.d(TAG, "Unable to read MediaFileMimeTypeForFile");
        }
        return fileName;
    }
相关推荐
安卓开发者11 小时前
Android RxJava 组合操作符实战:优雅处理多数据源
android·rxjava
阿华的代码王国11 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
一条上岸小咸鱼11 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
Jerry说前后端11 小时前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
alexhilton12 小时前
深入浅出着色器:极坐标系与炫酷环形进度条
android·kotlin·android jetpack
一条上岸小咸鱼18 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto18 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆18 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren18 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.1 天前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架