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;
    }
相关推荐
Cao_Shixin攻城狮2 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦5 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl7 小时前
Mysql测试题
android·adb
游戏开发爱好者89 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号10 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h11 小时前
Android原生Dialog
android
梁同学与Android12 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩13 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native
iHero13 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile17 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android