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;
    }
相关推荐
xiangzhihong82 小时前
使用Universal Links与Android App Links实现网页无缝跳转至应用
android·ios
车载应用猿2 小时前
基于Android14的CarService 启动流程分析
android
没有了遇见3 小时前
Android 渐变色实现总结
android
雨白6 小时前
Jetpack系列(四):精通WorkManager,让后台任务不再失控
android·android jetpack
mmoyula7 小时前
【RK3568 驱动开发:实现一个最基础的网络设备】
android·linux·驱动开发
sam.li8 小时前
WebView安全实现(一)
android·安全·webview
移动开发者1号9 小时前
Kotlin协程超时控制:深入理解withTimeout与withTimeoutOrNull
android·kotlin
程序员JerrySUN9 小时前
RK3588 Android SDK 实战全解析 —— 架构、原理与开发关键点
android·架构
移动开发者1号9 小时前
Java Phaser:分阶段任务控制的终极武器
android·kotlin
哲科软件18 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin