Java Code Examples for android.net.TrafficStats#getTotalRxPackets()

The following examples show how to use android.net.TrafficStats#getTotalRxPackets() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextCollector.java    From Mobilyzer with Apache License 2.0 4 votes vote down vote up
/**
   * called by the timer and return the current context info of device
   * 
   * @return a hash map that contains all the context data
   */
  @SuppressLint("NewApi")
  private HashMap<String, String> getCurrentContextInfo() {
    HashMap<String, String> currentContext = new HashMap<String, String>();;


    long intervalPktSend = 0;
    long intervalPktRecv = 0;
    long intervalSend = 0;
    long intervalRecv = 0;

    long sendBytes = TrafficStats.getTotalTxBytes();
    long recvBytes = TrafficStats.getTotalRxBytes();
    long sendPkt = TrafficStats.getTotalTxPackets();
    long recvPkt = TrafficStats.getTotalRxPackets();

    if (prevSend != -1 && prevRecv != -1) {
      intervalSend = sendBytes - prevSend;
      intervalRecv = recvBytes - prevRecv;
    }

    if (prevPktSend != -1 && prevPktRecv != -1) {
      intervalPktSend = sendPkt - prevPktSend;
      intervalPktRecv = recvPkt - prevPktRecv;
    }
    // we only return the context info if (1) it's the first time it gets called (2) we have
    // change in the amount of packet/byte sent/received.
    if (prevSend == -1 || prevRecv == -1 || prevPktSend == -1 || prevPktRecv == -1
        || intervalSend != 0 || intervalRecv != 0 || intervalPktSend != 0 || intervalPktRecv != 0) {
      currentContext.put("timestamp", (System.currentTimeMillis() * 1000) + "");
//      currentContext.put("rssi", phoneUtils.getCurrentRssi() + "");
      currentContext.put("inc_total_bytes_send", intervalSend + "");
      currentContext.put("inc_total_bytes_recv", intervalRecv + "");
      currentContext.put("inc_total_pkt_send", intervalPktSend + "");
      currentContext.put("inc_total_pkt_recv", intervalPktRecv + "");
      currentContext.put("battery_level", phoneUtils.getCurrentBatteryLevel() + "");
    }

    prevSend = sendBytes;
    prevRecv = recvBytes;
    prevPktSend = sendPkt;
    prevPktRecv = recvPkt;

    return currentContext;
  }
 
Example 2
Source File: NETUtils.java    From GTTools with MIT License 2 votes vote down vote up
/**
 * 获取整体的网络接收流量,包括wifi和Mobile
 * 
 * @return 总数据包数
 */
public static long getNetRxTotalPackets() {
	long total = TrafficStats.getTotalRxPackets();
	return total;
}
 
Example 3
Source File: NETUtils.java    From GTTools with MIT License 2 votes vote down vote up
/**
 * 获取整体的网络输出流量,包括wifi和Mobile
 * 
 * @return 总数据包数
 */
public static long getNetTxTotalPackets() {
	long total = TrafficStats.getTotalRxPackets();
	return total;
}