当前位置: 首页> 房产> 建筑 > 商丘网站制作公司_开发公司已经卖出生地_网站的seo如何优化_网络营销的营销方式是什么

商丘网站制作公司_开发公司已经卖出生地_网站的seo如何优化_网络营销的营销方式是什么

时间:2025/9/7 13:53:15来源:https://blog.csdn.net/LingoA/article/details/143039812 浏览次数:0次
商丘网站制作公司_开发公司已经卖出生地_网站的seo如何优化_网络营销的营销方式是什么

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

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

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

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");//通过反射调用getPathLog.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");//通过反射调用getPathLog.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");//通过反射调用getPathreturn (String) getMimeTypeForFile.invoke(fileName); //获取路径} catch (Exception e) {Log.e(TAG, e.getMessage());Log.d(TAG, "Unable to read MediaFileMimeTypeForFile");}return fileName;}

关键字:商丘网站制作公司_开发公司已经卖出生地_网站的seo如何优化_网络营销的营销方式是什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: