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;
    }
相关推荐
消失的旧时光-194333 分钟前
8方向控制圆盘View
android·前端
消失的旧时光-19431 小时前
摇杆控制View
android·kotlin
游戏开发爱好者81 小时前
iOS 抓包工具实战 开发者的工具矩阵与真机排查流程
android·ios·小程序·https·uni-app·iphone·webview
马 孔 多 在下雨10 小时前
安卓开发popupWindow的使用
android
asfdsfgas10 小时前
从 SSP 配置到 Gradle 同步:Android SDK 开发中 Manifest 合并冲突的踩坑记录
android
zhaoyufei13311 小时前
RK3399 11.0关闭调试串口改为普通RS232通信串口
android·驱动开发
消失的旧时光-194311 小时前
Kotlin 协程最佳实践:用 CoroutineScope + SupervisorJob 替代 Timer,实现优雅周期任务调度
android·开发语言·kotlin
UWA12 小时前
为什么Android游戏画面在30帧运行时有抖动现象
android·游戏
锐湃12 小时前
Android车载多媒体开发MediaSession框架理解
android
yueqc112 小时前
Android 通信机制简析
android·binder·handle