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;
}
}