反射调用android系统级API函数

来源:互联网 发布:域名记录类型 编辑:程序博客网 时间:2024/05/20 16:35
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 blocke.printStackTrace();}catch(Exception e){e.printStackTrace();}


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

这里不会讲反射意义,给出上面的例子,主要是为了说明,在系统中,凡是存在的类,我们都可以拿到其实例。从而调用其中的私有属性(非final)和私有方法,从而越过系统的限制。

0 0
原创粉丝点击