反射调用android系统级API函数

try {
			Class<?> mClass = Class.forName("com.android.server.wifi.WifiSettingsStore");
			Constructor con=mClass.getDeclaredConstructor(Context.class);
			if(!con.isAccessible()){
				con.setAccessible(true);
			}
			Object store = con.newInstance(this); 
			Method[] methods = mClass.getDeclaredMethods();
			Method method = null;
			for(Method m:methods){
				if(m.getName().equalsIgnoreCase("getPersistedScanAlwaysAvailable")){
					method = m;
					break;
				}
			}
			if(!method.isAccessible()){
				method.setAccessible(true);
			}
			Object a = method.invoke(store);
			Log.e("a", a.toString());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}


android系统级api中含有大量的类,当然这些底层类都会被public的api链接到,但有时候你可能须要突破系统的限制做一些事情,那这个时候反射就成了利器。

这里不会讲反射意义,给出上面的样例,主要是为了说明。在系统中,凡是存在的类,我们都能够拿到事实上例。

从而调用当中的私有属性(非final)和私有方法,从而越过系统的限制。

原文地址:https://www.cnblogs.com/brucemengbm/p/6880207.html