在基于2SIM MTK的电话上为每个SIM分别计算流量

时间:2015-10-13 12:36:28

标签: java android

我在GPlay上找到的任何应用都会为两个SIM计算流量,而在设置 - 数据使用情况下,每个SIM的流量都会计算在内。我用jd-gui来看看它是如何完成的,并且发现使用了来自私有API的类。

import android.net.INetworkStatsService;
import android.net.INetworkStatsService.Stub;
import android.net.INetworkStatsSession;
import android.net.NetworkPolicy;
import android.net.NetworkPolicyManager;
import android.net.NetworkStats;
import android.net.NetworkStats.Entry;
import android.net.NetworkStatsHistory;
import android.net.NetworkStatsHistory.Entry;
import android.net.NetworkTemplate;

我可以使用反射来使用它们吗?

更新。 我试过用反射。执行此代码会给我一个异常" java.lang.InstantiationException:无法实例化类android.net.INetworkStatsService"

        Class<Object> MyINetworkStatsService = null;
    try {
        MyINetworkStatsService = (Class<Object>) Class.forName("android.net.INetworkStatsService");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Object mStatsService = null;
    try {
        mStatsService = MyINetworkStatsService != null ? MyINetworkStatsService.newInstance() : null;
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

UPD。因为StatsService和StatsSession都是接口,所以我无法实例化它们并且应该使用Proxy。任何人都可以帮忙吗?

UPD。我已经付出了努力

        String id = getActiveSubscriberId(mContext);
    try {
        Object tmpl = null;
        long stats = 0;
        Class<?> a = Class.forName("android.net.NetworkTemplate");
        Class<?> b = Class.forName("android.net.INetworkStatsService");
        Method getState = b.getMethod("getNetworkTotalBytes", a, long.class, long.class);
        Method[] am = a.getDeclaredMethods();
        Method getTemplate = null;
        for (Method m : am) {
            if (m.getName().equalsIgnoreCase("buildTemplateMobileAll")) {
                getTemplate = m;
                break;
            }
        }
        if (getTemplate != null) {
            getTemplate.setAccessible(true);
            tmpl = getTemplate.invoke(a.getClass(), id);
        }
        Object object = Proxy.newProxyInstance(b.getClass().getClassLoader(), b.getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("getNetworkTotalBytes")) {
                    return method.invoke(args[0], args[1], args[2], args[3]);
                }
                throw new RuntimeException("no method found");
            }
        });            
        Object[] args = {b.getClass(), tmpl, Long.MIN_VALUE, Long.MAX_VALUE};
        stats = (long) ((b.getClass()) object).getState(args);
    } catch (ClassNotFoundException e0) {
    } catch (NoSuchMethodException e0) {
    } catch (IllegalAccessException e0) {
    } catch (InvocationTargetException e0) {
    }

但它表示存在错误&#34;方法调用预期&#34;在

stats = (long) ((b.getClass()) object).getState(args);

如果我像这样更改此字符串

stats = (long) ((b) object).getState(args);

我收到另一个错误 - &#34;未知类:&#39; b&#39;&#34;

0 个答案:

没有答案