计算在Android的每个应用程序的手机和WiFi数据的使用 [英] Calculating mobile and wifi data usage of each application in android

查看:514
本文介绍了计算在Android的每个应用程序的手机和WiFi数据的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有任何可能的方式来计算使用TrafficStats Android的每个应用程序的手机和WiFi的使用:(getUidRxBytes,getUidTxBytes,getTotalRxbytes,getTotalTXbytes,getMobileRxBytes,getMobileTxBytes)的方法呢?我知道一定有这样做的3G看门狗和其他应用程序提供这些细节的一些方法。

Is there any possible way for calculating mobile and wifi usage of each application in android using TrafficStats' : (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) methods ? I know there must be some way of doing that as 3G watchdog and some other application provide these details.

任何人都可以帮助吗?谢谢

Can anybody help please ? Thanks

推荐答案

经过长期斗争,我能找到获取数据在任何界面在Android中每个已安装的应用解决方案 设备。

After a long struggle,I am able to find the Solution for getting data over any interface for each installed Application in android device.

由于Android提供TrafficStats蜜蜂,但是这些API是提供完井统计中的数据为每个应用程序UID,因为设备启动和偶数 API是不支持获取数据通过用于特定应用的任何接口。 即使我们依靠过TraffiucStates的API,我们得到了一个新的数据statstics为每个应用程序。

As Android provides TrafficStats Apis but these APIs are providing comple Data stastics for each app uid since device boot and Even APIs are not supporting to get the data over any interface for a particular application. Even if we rely over TraffiucStates APIS ,we get a new data statstics for each Application.

所以我想用隐藏的API来使用这个..

So I thought to use the hidden APIs to USe this..

在这里,我提的步骤获得的数据statstics为每个应用程序在Android中的任何界面......

Here I am mentioning the Steps to get the data statstics for each application over any Interface in Android...

  1. Estabalish一个INetworkStatsSession会议

import android.net.INetworkStatsSession;

INetworkStatsSession mStatsSession = mStatsService.openSession();

  • 创建网络Templeate根据要测量interafce ..

    import static android.net.NetworkTemplate.buildTemplateEthernet;
    import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
    import static android.net.NetworkTemplate.buildTemplateMobile4g;
    import static android.net.NetworkTemplate.buildTemplateMobileAll;
    import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
    
    import android.net.NetworkTemplate;
    
    private NetworkTemplate mTemplate;
    
    mTemplate = buildTemplateMobileAll(getActiveSubscriberId(this
                .getApplicationContext()));
    

  • GetActive SubcriberID:

    private static String getActiveSubscriberId(Context context) {
        final TelephonyManager tele = TelephonyManager.from(context);
        final String actualSubscriberId = tele.getSubscriberId();
        return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
    }
    

  • 收集相应的应用程​​序的网络的tory BYT通过应用程序的UID ...

    private NetworkStatsHistory collectHistoryForUid(NetworkTemplate template,
            int uid, int set) throws RemoteException {
        final NetworkStatsHistory history = mStatsSession.getHistoryForUid(
                template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
        return history;
    
    }
    

  • 得到总消费数据:

    public void showConsuption(int UID){
        NetworkStatsHistory history = collectHistoryForUid(mTemplate, UID, SET_DEFAULT);
        Log.i(DEBUG_TAG, "load:::::SET_DEFAULT:.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
        history = collectHistoryForUid(mTemplate, 10093, SET_FOREGROUND);
        Log.i(DEBUG_TAG, "load::::SET_FOREGROUND::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
        history = collectHistoryForUid(mTemplate, 10093, SET_ALL);
        Log.i(DEBUG_TAG, "load::::SET_ALL::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
    }
    

  • 这篇关于计算在Android的每个应用程序的手机和WiFi数据的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆