android.os.SystemProperties反射帮助类
来源:互联网 发布:vr的前景 知乎 编辑:程序博客网 时间:2024/05/17 21:34
package com.viking;import java.lang.reflect.Method;import android.content.Context;/** * @author Viking.Den * * @email vikingden@live.com * * @time 2014-7-17 */public class SystemPropertiesProxy{/** * This class cannot be instantiated */private SystemPropertiesProxy(){} /** * Get the value for the given key. * @return an empty string if the key isn't found * @throws IllegalArgumentException if the key exceeds 32 characters */ public static String get(Context context, String key) throws IllegalArgumentException { String ret= ""; try{ ClassLoader cl = context.getClassLoader(); @SuppressWarnings("rawtypes") Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[1]; paramTypes[0]= String.class; Method get = SystemProperties.getMethod("get", paramTypes); //Parameters Object[] params= new Object[1]; params[0]= new String(key); ret= (String) get.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){ ret= ""; } return ret; } /** * Get the value for the given key. * @return if the key isn't found, return def if it isn't null, or an empty string otherwise * @throws IllegalArgumentException if the key exceeds 32 characters */ public static String get(Context context, String key, String def) throws IllegalArgumentException { String ret= def; try{ ClassLoader cl = context.getClassLoader(); @SuppressWarnings("rawtypes") Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[2]; paramTypes[0]= String.class; paramTypes[1]= String.class; Method get = SystemProperties.getMethod("get", paramTypes); //Parameters Object[] params= new Object[2]; params[0]= new String(key); params[1]= new String(def); ret= (String) get.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){ ret= def; } return ret; } /** * Get the value for the given key, and return as an integer. * @param key the key to lookup * @param def a default value to return * @return the key parsed as an integer, or def if the key isn't found or * cannot be parsed * @throws IllegalArgumentException if the key exceeds 32 characters */ public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException { Integer ret= def; try{ ClassLoader cl = context.getClassLoader(); @SuppressWarnings("rawtypes") Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[2]; paramTypes[0]= String.class; paramTypes[1]= int.class; Method getInt = SystemProperties.getMethod("getInt", paramTypes); //Parameters Object[] params= new Object[2]; params[0]= new String(key); params[1]= new Integer(def); ret= (Integer) getInt.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){ ret= def; } return ret; } /** * Get the value for the given key, and return as a long. * @param key the key to lookup * @param def a default value to return * @return the key parsed as a long, or def if the key isn't found or * cannot be parsed * @throws IllegalArgumentException if the key exceeds 32 characters */ public static Long getLong(Context context, String key, long def) throws IllegalArgumentException { Long ret= def; try{ ClassLoader cl = context.getClassLoader(); @SuppressWarnings("rawtypes") Class SystemProperties= cl.loadClass("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[2]; paramTypes[0]= String.class; paramTypes[1]= long.class; Method getLong = SystemProperties.getMethod("getLong", paramTypes); //Parameters Object[] params= new Object[2]; params[0]= new String(key); params[1]= new Long(def); ret= (Long) getLong.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){ ret= def; } return ret; } /** * Get the value for the given key, returned as a boolean. * Values 'n', 'no', '0', 'false' or 'off' are considered false. * Values 'y', 'yes', '1', 'true' or 'on' are considered true. * (case insensitive). * If the key does not exist, or has any other value, then the default * result is returned. * @param key the key to lookup * @param def a default value to return * @return the key parsed as a boolean, or def if the key isn't found or is * not able to be parsed as a boolean. * @throws IllegalArgumentException if the key exceeds 32 characters */ public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException { Boolean ret= def; try{ ClassLoader cl = context.getClassLoader(); @SuppressWarnings("rawtypes") Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[2]; paramTypes[0]= String.class; paramTypes[1]= boolean.class; Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes); //Parameters Object[] params= new Object[2]; params[0]= new String(key); params[1]= new Boolean(def); ret= (Boolean) getBoolean.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){ ret= def; } return ret; } /** * Set the value for the given key. * @throws IllegalArgumentException if the key exceeds 32 characters * @throws IllegalArgumentException if the value exceeds 92 characters */ public static void set(Context context, String key, String val) throws IllegalArgumentException { try{ @SuppressWarnings("rawtypes") Class SystemProperties = Class.forName("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes= new Class[2]; paramTypes[0]= String.class; paramTypes[1]= String.class; Method set = SystemProperties.getMethod("set", paramTypes); //Parameters Object[] params= new Object[2]; params[0]= new String(key); params[1]= new String(val); set.invoke(SystemProperties, params); }catch( IllegalArgumentException iAE ){ throw iAE; }catch( Exception e ){} }}
0 0
- android.os.SystemProperties反射帮助类
- 没有android.os.SystemProperties类??
- Android.os包下没有SystemProperties类
- 如何使用android.os.SystemProperties
- 如何使用android.os.SystemProperties
- 如何使用android.os.SystemProperties
- 如何使用android.os.SystemProperties
- android.os.SystemProperties和android.os.Environment
- 如何使用android.os.SystemProperties的方法
- 如何使用android.os.SystemProperties的方法
- 如何使用android.os.SystemProperties的方法
- 关于android.os.SystemProperties的使用
- 如何使用android.os.SystemProperties的方法
- 如何使用android.os.SystemProperties的方法
- 如何使用android.os.SystemProperties的方法
- 无法找到android.os.SystemProperties解决办法
- 如何反射获取Android SystemProperties 的字段
- Error:(10, 18) 错误: 找不到符号 符号: 类 SystemProperties 位置: 程序包 android.os
- vc2013 atomic_int and atomic<int> bug
- kendoui仪表盘和柱状图 示例
- 适合2015年开发的10个新锐框架(上)
- 图论基础
- Populating Next Right Pointers in Each Node
- android.os.SystemProperties反射帮助类
- 莆籍北漂青年刘永宗在京举行个人作品签售会(图)
- 【bzoj 2330】 糖果 candy
- Swift初体验
- Microsoft Office Project 2010 操作手册
- nodejs中exports与module.exports的区别
- POJ1466 Girls and Boys
- 利用 squid 反向代理提高网站性能
- Request中Attribute 和 Parameter 的区别