Android network data sample

import android.app.usage.NetworkStats;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;


public class NetworkManager extends Manager {
    private NetworkStatsManager networkStatsManager;

    public NetworkManager(Config config) {
        super(config);

        networkStatsManager = (NetworkStatsManager) SDKUtil.getAppContext().getSystemService(Context.NETWORK_STATS_SERVICE);
    }

    /**
     * 设备启动以来,总的数据流量统计。
     * 开发者可以每隔一定时间,比如1秒,轮询该函数,从中提取设备发送或接受的流量数据,某个时间点一秒后的数据减去当前时间的流量数据,即可得到设备的在一秒内的流量统计。
     *
     * @return
     */
    @Override
    public Detail getData() {
        Detail detail = new Detail();

        if (this.getConfig() == null) {
            try {
                //获取通过Mobile连接收到的字节总数,但不包含WiFi。
                long mobileRxBytes = TrafficStats.getMobileRxBytes();
                detail.data().put("mobileRxBytes", mobileRxBytes);

                //获取Mobile连接收到的数据包总数。
                long mobileRxPackets = TrafficStats.getMobileRxPackets();
                detail.data().put("mobileRxPackets", mobileRxPackets);

                //Mobile发送的总字节数。
                long mobileTxBytes = TrafficStats.getMobileTxBytes();
                detail.data().put("mobileTxBytes", mobileTxBytes);

                //Mobile发送的总数据包数。
                long mobileTxPackets = TrafficStats.getMobileTxPackets();
                detail.data().put("mobileTxPackets", mobileTxPackets);

                //获取总的接受字节数,包含Mobile和WiFi等。
                long totalRxBytes = TrafficStats.getTotalRxBytes();
                detail.data().put("totalRxBytes", totalRxBytes);

                //总的接受数据包数,包含Mobile和WiFi等。
                long totalRxPackets = TrafficStats.getTotalRxPackets();
                detail.data().put("totalRxPackets", totalRxPackets);

                //总的发送字节数,包含Mobile和WiFi等。
                long totalTxBytes = TrafficStats.getTotalTxBytes();
                detail.data().put("totalTxBytes", totalTxBytes);

                //发送的总数据包数,包含Mobile和WiFi等。
                long totalTxPackets = TrafficStats.getTotalTxPackets();
                detail.data().put("totalTxPackets", totalTxPackets);
            } catch (Exception e) {
                e.printStackTrace();
            }

            detail.time = SDKUtil.getInstance().formatTime(System.currentTimeMillis());
        }

        return detail;
    }

    /**
     * 获取指定uid的接收字节数 。
     * 自启动以来。
     *
     * @param uid
     * @return
     */
    public long getUidRxBytes(int uid) {
        return TrafficStats.getUidRxBytes(uid);
    }

    /**
     * 获取指定uid的发送字节数。
     * 自启动以来。
     *
     * @param uid
     * @return
     */
    public long getUidTxBytes(int uid) {
        return TrafficStats.getUidTxBytes(uid);
    }

    /**
     * 查询指定网络类型在某时间间隔内的总的流量统计信息。
     *
     * @param networkType  {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
     * @param subscriberId
     * @param startTime
     * @param endTime
     * @return
     */
    public NetworkStats.Bucket querySummaryForDevice(int networkType, String subscriberId, long startTime, long endTime) {
        try {
            return networkStatsManager.querySummaryForDevice(networkType, subscriberId, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //查询某uid在指定网络类型和时间间隔内的流量统计信息。
    public NetworkStats queryDetailsForUid(int networkType, String subscriberId, long startTime, long endTime, int uid) {
        return networkStatsManager.queryDetailsForUid(networkType, subscriberId, startTime, endTime, uid);
    }

    //查询指定网络类型在某时间间隔内的详细的流量统计信息(每个uid)。
    public NetworkStats queryDetails(int networkType, String subscriberId, long startTime, long endTime) {
        try {
            return networkStatsManager.queryDetails(networkType, subscriberId, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //获取网络的状态信息。
    public NetworkInfo getNetworkInfo() {
        ConnectivityManager connectionManager = (ConnectivityManager) SDKUtil.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        return networkInfo;
    }
}

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值