获取原生Settings 流量统计接口

通过系统提供AIDL接口给,第三方应用调用,获取流量使用情况。

获取当月流量使用情况:

/**
 * Created by ybf
 */
 public List inivData() {
        INetworkStatsService mStatsService = null;
        INetworkStatsSession mStatsSession = null;
        NetworkTemplate mTemplate = null;
        NetworkStats sta = null;
        List<AppInfoBean>  lists = new ArrayList<>();
        try {
            long start = getStarttime();
            long end =System.currentTimeMillis();
            Log.e(TAG, "[start]=" + start + "[end]=" + end);
            mStatsService = INetworkStatsService.Stub.asInterface(ServiceManager.getService("netstats"));
            Log.e(TAG, "[mStatsService]="+mStatsService);
            mStatsSession = mStatsService.openSession();
            Log.e(TAG, "[mStatsSession]="+mStatsSession);
            mTemplate = buildTemplateMobileAll(getActiveSubscriberId(mContext));
            Log.e(TAG, "[getActiveSubscriberId(mContext)]="+getActiveSubscriberId(mContext));
            Log.e(TAG, "[mTemplate]="+mTemplate);
            sta = mStatsSession.getSummaryForAllUid(mTemplate, start, end, true);
            NetworkStats.Entry entry = null;
            int size = sta != null ? sta.size() : 0;
            Log.e(TAG, "[service-size-]" + size);
            for (int i = 0; i < size; i++) {
                entry = sta.getValues(i, entry);
                final int uid = entry.uid;
                long totalapp = entry.rxBytes + entry.txBytes;
                String sumtotal = Formatter.formatFileSize(mContext, totalapp);
                String name = null;
                switch (uid) {
                    case android.os.Process.SYSTEM_UID:
                        name = res.getString(R.string.process_kernel_label);
                        break;
                    case -4:
                        name = res.getString(R.string.data_usage_uninstalled_apps);
                        break;
                    case -5:
                        name = res.getString(R.string.tether_settings_title_all);
                        break;
                    default:
                       if(uid< 9999){
                            name = "null";
                        }else {
                            try {
                                name = getProgramNameByPackageName(mContext, uid);
                            } catch (Exception e) {
                                Log.e(TAG, "package name is null");
                            }
                        }
                        break;
                }
                AppInfoBean appInfo = new AppInfoBean();
                appInfo.setName(name);
                appInfo.setTotal(sumtotal);
                appInfo.setUid(uid);
                lists.add(appInfo);
                Log.e(TAG, "[uid]=" + uid + "[name]=" + name + "[total]=" + sumtotal);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            e.toString();
        }
        return  lists;
    }

获取历史流量使用情况:

/**
 * Created by ybf
 */
 public List inivDataHistory(long start ,long end) {
        INetworkStatsService mStatsService = null;
        INetworkStatsSession mStatsSession = null;
        NetworkTemplate mTemplate = null;
        NetworkStats sta = null;
        List<AppInfoBean>  lists = new ArrayList<>();
        try {
            if(start == 0){
                start = getStarttime();
            }
           if(end == 0){
               end =System.currentTimeMillis();
           }
            Log.e(TAG, "[start]=" + start + "[end]=" + end);
            mStatsService = INetworkStatsService.Stub.asInterface(ServiceManager.getService("netstats"));
            Log.e(TAG, "[mStatsService]="+mStatsService);
            mStatsSession = mStatsService.openSession();
            Log.e(TAG, "[mStatsSession]="+mStatsSession);
            mTemplate = buildTemplateMobileAll(getActiveSubscriberId(mContext));
            Log.e(TAG, "[getActiveSubscriberId(mContext)]="+getActiveSubscriberId(mContext));
            Log.e(TAG, "[mTemplate]="+mTemplate);
            sta = mStatsSession.getSummaryForAllUid(mTemplate, start, end, true);
            NetworkStats.Entry entry = null;
            int size = sta != null ? sta.size() : 0;
            Log.e(TAG, "[service-size-]" + size);
            for (int i = 0; i < size; i++) {
                entry = sta.getValues(i, entry);
                final int uid = entry.uid;
                long totalapp = entry.rxBytes + entry.txBytes;
                String sumtotal = Formatter.formatFileSize(mContext, totalapp);
                String name = null;
                switch (uid) {
                    case android.os.Process.SYSTEM_UID:
                        name = res.getString(R.string.process_kernel_label);
                        break;
                    case -4:
                        name = res.getString(R.string.data_usage_uninstalled_apps);
                        break;
                    case -5:
                        name = res.getString(R.string.tether_settings_title_all);
                        break;
                    default:
                        if(uid< 9999){
                            name = "null";
                        }else {
                            try {
                                name = getProgramNameByPackageName(mContext, uid);
                            } catch (Exception e) {
                                Log.e(TAG, "package name is null");
                            }
                        }
                        break;
                }
                AppInfoBean appInfo = new AppInfoBean();
                appInfo.setName(name);
                appInfo.setTotal(sumtotal);
                appInfo.setUid(uid);
                lists.add(appInfo);
                Log.e(TAG, "[uid]=" + uid + "[name]=" + name + "[total]=" + sumtotal);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return  lists;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FW_G8Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值