Android反射

来源:互联网 发布:c4d mac版 编辑:程序博客网 时间:2024/06/06 05:01
try {//包类名            Class<?> clazz = Class.forName("android.net.DataUsageInfo");//方法名,参数 这里四个参数int, boolean, long, long            Method methodWiFi = clazz.getMethod("getWifiDataByUID",                    new Class[] { int.class, boolean.class, long.class,                            long.class });//访问有参构造函数,这里参数 Context            Constructor constructorWiFi = clazz.getConstructor(Context.class);//new 实例            Object objectWiFi = constructorWiFi.newInstance(this);//传递参数            long obj = (long) methodWiFi.invoke(objectWiFi, -1, true, 0, 0);            long obj1 = (long) methodWiFi.invoke(objectWiFi, -1, false,                    1471060800000l, 1471064400000l);            long obj2 = (long) methodWiFi.invoke(objectWiFi, 10045, true, 0, 0);            long obj3 = (long) methodWiFi.invoke(objectWiFi, 10045, false,                    1471060800000l, 1471064400000l);            android.util.Log.i("mijiee",                    "obj: " + Formatter.formatFileSize(this, obj) + "obj1: "                            + Formatter.formatFileSize(this, obj1) + "obj2: "                            + Formatter.formatFileSize(this, obj2) + "obj3: "                            + Formatter.formatFileSize(this, obj3));        } catch (Exception e) {            android.util.Log.i("mijiee", "e: " + e.toString());        }
0 0