Android 反射调用SystemProperties

Posted peak wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 反射调用SystemProperties相关的知识,希望对你有一定的参考价值。

import java.lang.reflect.Method;

public class SystemProperties
{

	public static String get(String key)
	{
		String ret = null;
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("get", new Class[] { String.class });
			mthd.setAccessible(true);
			Object obj = mthd.invoke(clazz, new Object[] { key });
			if (obj != null && obj instanceof String)
			{
				ret = (String) obj;

			}

		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		return ret;

	}

	public static String get(String key, String def)
	{
		String ret = def;
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("get", new Class[] { String.class, String.class });
			mthd.setAccessible(true);
			Object obj = mthd.invoke(clazz, new Object[] { key, def });
			if (obj != null && obj instanceof String)
			{
				ret = (String) obj;

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();

		}

		return ret;
	}
	
	public static boolean getBoolean(String key, boolean def)
	{
		boolean ret = def;
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("getBoolean", new Class[] { String.class, boolean.class });
			mthd.setAccessible(true);
			Object obj = mthd.invoke(clazz, new Object[] { key, def });
			if (obj != null && obj instanceof Boolean)
			{
				ret = (Boolean) obj;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return ret;
	}
	
	public static int getInt(String key, int def)
	{
		int ret = def;
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("getInt", new Class[] { String.class, int.class });
			mthd.setAccessible(true);
			Object obj = mthd.invoke(clazz, new Object[] { key, def });
			if (obj != null && obj instanceof Integer)
			{
				ret = (Integer) obj;

			}

		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		return ret;

	}
	
	public static long getLong(String key, long def)
	{
		long ret = def;
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("getLong", new Class[] { String.class, long.class });
			mthd.setAccessible(true);
			Object obj = mthd.invoke(clazz, new Object[] { key, def });
			if (obj != null && obj instanceof Long)
			{
				ret = (Long) obj;

			}

		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		return ret;
	}
	
	public static void set(String key, String value)
	{
		try
		{
			Class<?> clazz = Class.forName("android.os.SystemProperties");
			Method mthd = clazz.getMethod("set", new Class[] { String.class, String.class });
			mthd.setAccessible(true);
			mthd.invoke(clazz, new Object[] { key, value });
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

以上是关于Android 反射调用SystemProperties的主要内容,如果未能解决你的问题,请参考以下文章

反射调用android系统级API函数

小花招解决Android 9 Pie 不能反射隐藏API限制

从Java反射机制到Android注解框架

Android开发之通过反射获取到Android隐藏的方法

Java反射在Android中的使用

Android EventBus框架之源码简单解析