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;
    }
相关推荐
CYRUS_STUDIO2 小时前
一文搞懂 Frida Stalker:对抗 OLLVM 的算法还原利器
android·逆向·llvm
zcychong3 小时前
ArrayMap、SparseArray和HashMap有什么区别?该如何选择?
android·面试
CYRUS_STUDIO3 小时前
Frida Stalker Trace 实战:指令级跟踪与寄存器变化监控全解析
android·逆向
ace望世界8 小时前
android的Parcelable
android
顾林海8 小时前
Android编译插桩之AspectJ:让代码像特工一样悄悄干活
android·面试·性能优化
叽哥8 小时前
Flutter Riverpod上手指南
android·flutter·ios
循环不息优化不止8 小时前
安卓开发设计模式全解析
android
诺诺Okami8 小时前
Android Framework-WMS-层级结构树
android
alexhilton19 小时前
面向开发者的系统设计:像建筑师一样思考
android·kotlin·android jetpack
CYRUS_STUDIO1 天前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向