首页 > 其他分享 >利用反射获取android.os.SystemProperties 属性

利用反射获取android.os.SystemProperties 属性

时间:2023-01-09 14:34:59浏览次数:54  
标签:String SystemProperties paramTypes params key android os def

记录一下项目用到的工具类
import android.content.Context; import java.lang.reflect.Method; public class SystemPropertyUtil { /** * 根据给定Key获取值. * * @return 如果不存在该key则返回空字符串 * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 */ 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"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[1]; paramTypes[0] = String.class; Method get = SystemProperties.getMethod("get", paramTypes); // 参数 Object[] params = new Object[1]; params[0] = key; ret = (String) get.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { ret = ""; // TODO } return ret; } /** * 根据Key获取值. * * @return 如果key不存在, 并且如果def不为空则返回def否则返回空字符串 * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 */ 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"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[2]; paramTypes[0] = String.class; paramTypes[1] = String.class; Method get = SystemProperties.getMethod("get", paramTypes); // 参数 Object[] params = new Object[2]; params[0] = key; params[1] = def; ret = (String) get.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { ret = def; // TODO } return ret; } /** * 根据给定的key返回int类型值. * * @param key * 要查询的key * @param def * 默认返回值 * @return 返回一个int类型的值, 如果没有发现则返回默认值 * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 */ 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"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[2]; paramTypes[0] = String.class; paramTypes[1] = int.class; Method getInt = SystemProperties.getMethod("getInt", paramTypes); // 参数 Object[] params = new Object[2]; params[0] = key; params[1] = new Integer(def); ret = (Integer) getInt.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { ret = def; // TODO } return ret; } /** * 根据给定的key返回long类型值. * * @param key * 要查询的key * @param def * 默认返回值 * @return 返回一个long类型的值, 如果没有发现则返回默认值 * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 */ 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"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[2]; paramTypes[0] = String.class; paramTypes[1] = long.class; Method getLong = SystemProperties.getMethod("getLong", paramTypes); // 参数 Object[] params = new Object[2]; params[0] = key; params[1] = new Long(def); ret = (Long) getLong.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { ret = def; // TODO } return ret; } /** * 根据给定的key返回boolean类型值. 如果值为 'n', 'no', '0', 'false' or 'off' 返回false. * 如果值为'y', 'yes', '1', 'true' or 'on' 返回true. 如果key不存在, 或者是其它的值, 则返回默认值. * * @param key * 要查询的key * @param def * 默认返回值 * @return 返回一个boolean类型的值, 如果没有发现则返回默认值 * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 */ 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"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[2]; paramTypes[0] = String.class; paramTypes[1] = boolean.class; Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes); // 参数 Object[] params = new Object[2]; params[0] = key; params[1] = new Boolean(def); ret = (Boolean) getBoolean.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { ret = def; // TODO } return ret; } /** * 根据给定的key和值设置属性, 该方法需要特定的权限才能操作. * * @throws IllegalArgumentException * 如果key超过32个字符则抛出该异常 * @throws IllegalArgumentException * 如果value超过92个字符则抛出该异常 */ public static void set(Context context, String key, String val) throws IllegalArgumentException { try { @SuppressWarnings("rawtypes") Class SystemProperties = Class.forName("android.os.SystemProperties"); // 参数类型 @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[2]; paramTypes[0] = String.class; paramTypes[1] = String.class; Method set = SystemProperties.getMethod("set", paramTypes); // 参数 Object[] params = new Object[2]; params[0] = key; params[1] = val; set.invoke(SystemProperties, params); } catch (IllegalArgumentException iAE) { throw iAE; } catch (Exception e) { // TODO } } }

 

标签:String,SystemProperties,paramTypes,params,key,android,os,def
From: https://www.cnblogs.com/mhzf/p/17036942.html

相关文章

  • postman.多环境配置及应用
    在使用postman时,同一个API接口,往往会涉及到至少二个不同的应用服务地址,如:本机开发环境,客户服务环境>> 为简化 不同环境的切换,在postman中提供了环境配置功能>> 环境配......
  • Android开发工具之DDMS
         DDMS的全称是DalvikDebugMonitorService,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息、Logcat、广播状态信息、模拟电话呼叫......
  • Docker-compose
    一、Docker-compose简介Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。Docker-Compose将所管理的容器分为三层,分别是工程(project),服务......
  • 前后端分离开发模式下的接口测试工具推荐|第1万01次卸载postman , 我要用eolink
    ❤️作者主页:​​微凉秋意​​✅作者简介:后端领域优质创作者......
  • 分享用Adobe Air向iOS移植游戏的经验
    分享用AdobeAir向iOS移植游戏的经验​​http://gamerboom.com/archives/47931​​发布时间:2012-02-2117:04:42Tags:​​AdobeAir​​​,​​​iOS移植游......
  • 【探花交友】阿里云OSS、百度人脸识别
    文章目录​​1、完善用户信息​​​​1.1、阿里云OSS​​​​1.2、百度人脸识别​​1、完善用户信息用户在首次登录时需要完善个人信息,包括性别、昵称、生日、城市、头像等......
  • 部署 anolisOS 8.6
    官方下载地址:https://openanolis.cn/download本次下载镜像:AnolisOS-8.6-x86_64-minimal.iso(dvd的太大,10G)安装界面友好,按步骤往下走就行了#配置网络nmtuivi/etc/s......
  • 搭建windows下的android开发环境
    搭建windows下面的android开发环境一般需要以下工具或软件:1. ​​jdk​​(要求jdk5或jdk6)2. ​​eclipse​​​(要求eclipse3.4或eclipse3.5)(​​汉化包下载​​)3. ......
  • 【linux】linux Centos8系统,防火墙配置常用命令,systemctl 和firewall
    本文环境:Linux系统CentOS8.264bitCentOS7版本及以上版本较centos6有较大改动,例如:采用systemctl命令来开启service,它是服务管理中主要的工具,融合了之前service和chkconf......
  • 【linux】RabbitMQ学习-vhost 详解
    vhost本质上是一个mini版的RabbitMQ服务器,拥有自己的队列、绑定、交换器和权限控制;vhost通过在各个实例间提供逻辑上分离,允许你为不同应用程序安全保密地运行数据;vhost是......