下面列出了android.net.TrafficStats#UNSUPPORTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
double totalBytesReceived = TrafficStats.getUidRxBytes(uid);
double totalBytesSent = TrafficStats.getUidTxBytes(uid);
if (totalBytesReceived == TrafficStats.UNSUPPORTED || totalBytesSent == TrafficStats.UNSUPPORTED) {
Log.w(TAG, "The use of TrafficStats is not supported on this device.");
return;
}
if (previousReceived >= 0 && previousSent >= 0) {
received = (totalBytesReceived - previousReceived) / intervalSeconds;
sent = (totalBytesSent - previousSent) / intervalSeconds;
notifyObservers();
}
previousReceived = totalBytesReceived;
previousSent = totalBytesSent;
handler.postDelayed(this, intervalMilliseconds);
}
@VisibleToAvoidSynthetics
synchronized void updateTotalBytes() {
long currentTotalTxBytes = TrafficStats.getUidTxBytes(UID);
long currentTotalRxBytes = TrafficStats.getUidRxBytes(UID);
if (currentTotalRxBytes == TrafficStats.UNSUPPORTED
|| currentTotalTxBytes == TrafficStats.UNSUPPORTED) {
mIsValid = false;
return;
}
int prefix = mCurrentNetworkType == TYPE_WIFI ? WIFI : MOBILE;
long lastTotalTxBytes = mTotalBytes[TX | MOBILE] + mTotalBytes[TX | WIFI];
long lastTotalRxBytes = mTotalBytes[RX | MOBILE] + mTotalBytes[RX | WIFI];
mTotalBytes[TX | prefix] += currentTotalTxBytes - lastTotalTxBytes;
mTotalBytes[RX | prefix] += currentTotalRxBytes - lastTotalRxBytes;
}
private long getNetSpeed(Context context) {
if (context == null) {
return 0;
}
long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
long nowTimeStamp = System.currentTimeMillis();
long calculationTime = (nowTimeStamp - lastTimeStamp);
if (calculationTime == 0) {
return calculationTime;
}
//毫秒转换
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / calculationTime);
lastTimeStamp = nowTimeStamp;
lastTotalRxBytes = nowTotalRxBytes;
return speed;
}
private long getNetSpeed(Context context) {
if (context == null) {
return 0;
}
long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
long nowTimeStamp = System.currentTimeMillis();
long calculationTime = (nowTimeStamp - lastTimeStamp);
if (calculationTime == 0) {
return calculationTime;
}
//毫秒转换
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / calculationTime);
lastTimeStamp = nowTimeStamp;
lastTotalRxBytes = nowTotalRxBytes;
return speed;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSupported = (TextView) findViewById(R.id.tvSupported);
tvDataUsageWiFi = (TextView) findViewById(R.id.tvDataUsageWiFi);
tvDataUsageMobile = (TextView) findViewById(R.id.tvDataUsageMobile);
tvDataUsageTotal = (TextView) findViewById(R.id.tvDataUsageTotal);
if (TrafficStats.getTotalRxBytes() != TrafficStats.UNSUPPORTED && TrafficStats.getTotalTxBytes() != TrafficStats.UNSUPPORTED) {
handler.postDelayed(runnable, 0);
initAdapter();
lvApplications = (ListView) findViewById(R.id.lvInstallApplication);
lvApplications.setAdapter(adapterApplications);
} else {
tvSupported.setVisibility(View.VISIBLE);
}
}
@SuppressLint("SetTextI18n")
private void updateData() {
if (App.ClementineConnection == null) {
return;
}
long diff = new Date().getTime() - App.ClementineConnection.getStartTime();
String dateFormat = String.format(Locale.US, "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(diff),
TimeUnit.MILLISECONDS.toMinutes(diff) % 60,
TimeUnit.MILLISECONDS.toSeconds(diff) % 60
);
tv_time.setText(dateFormat);
int uid = getActivity().getApplicationInfo().uid;
if (TrafficStats.getUidRxBytes(uid) == TrafficStats.UNSUPPORTED) {
tv_traffic.setText(R.string.connection_traffic_unsupported);
} else {
String tx = Utilities.humanReadableBytes(
TrafficStats.getUidTxBytes(uid) - App.ClementineConnection.getStartTx(), true);
String rx = Utilities.humanReadableBytes(
TrafficStats.getUidRxBytes(uid) - App.ClementineConnection.getStartRx(), true);
long total = TrafficStats.getUidTxBytes(uid) - App.ClementineConnection.getStartTx() +
TrafficStats.getUidRxBytes(uid) - App.ClementineConnection.getStartRx();
if (TimeUnit.MILLISECONDS.toSeconds(diff) != 0) {
long a = total / TimeUnit.MILLISECONDS.toSeconds(diff);
String perSecond = Utilities.humanReadableBytes(a, true);
tv_traffic.setText(tx + " / " + rx + " (" + perSecond + "/s)");
}
}
}
@Override
public int start() {
if ((trafficRxStart = TrafficStats.getTotalRxBytes()) == TrafficStats.UNSUPPORTED) {
return SERVICE_NOT_SUPPORTED;
}
running = true;
trafficTxStart = TrafficStats.getTotalTxBytes();
return SERVICE_START_OK;
}
public void reset() {
startTotalBytes = 0;
lastTotalBytes = TrafficStats.UNSUPPORTED;
lastTotalTxBytes = TrafficStats.UNSUPPORTED;
lastTotalRxBytes = TrafficStats.UNSUPPORTED;
refreshPeriod = 1000;
reqStop = false;
}
@Override
public void run() {
while (true) {
if (reqStop) {
if (callback != null) {
callback.afterStop();
}
return;
}
if (lastTotalTxBytes == TrafficStats.UNSUPPORTED) {
lastTotalTxBytes = TrafficStats.getTotalTxBytes();
lastTotalRxBytes = TrafficStats.getTotalRxBytes();
lastTotalBytes = lastTotalTxBytes + lastTotalRxBytes;
SystemClock.sleep(refreshPeriod);
continue;
}
long total = TrafficStats.getTotalTxBytes();
netSpeedUp = (int) ((total - lastTotalTxBytes) * 1000 / refreshPeriod);
lastTotalTxBytes = total;
total = TrafficStats.getTotalRxBytes();
netSpeedDown = (int) ((total - lastTotalRxBytes) * 1000 / refreshPeriod);
lastTotalRxBytes = total;
total = lastTotalTxBytes + lastTotalRxBytes;
netSpeed = (int) ((total - lastTotalBytes) * 1000 / refreshPeriod);
// recentBytes[(++recentIndex) % recentBytes.length] = (int) (total - lastTotalBytes);
lastTotalBytes = total;
usedBytes = lastTotalBytes - startTotalBytes;
if (callback != null) {
callback.onUpdate(netSpeed, netSpeedUp, netSpeedDown, usedBytes);
}
SystemClock.sleep(refreshPeriod);
}
}
@Override
public int start() {
uid = Process.myUid();
if ((trafficRxStart = TrafficStats.getUidRxBytes(uid)) == TrafficStats.UNSUPPORTED) {
return SERVICE_NOT_SUPPORTED;
}
running = true;
trafficTxStart = TrafficStats.getUidTxBytes(uid);
return SERVICE_START_OK;
}
/**
* @return Number of bytes transmitted since device boot. Counts packets across all network
* interfaces, and always increases monotonically since device boot. Statistics are
* measured at the network layer, so they include both TCP and UDP usage.
*/
@CalledByNative
private static long getTotalTxBytes() {
long bytes = TrafficStats.getTotalTxBytes();
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
/**
* @return Number of bytes received since device boot. Counts packets across all network
* interfaces, and always increases monotonically since device boot. Statistics are
* measured at the network layer, so they include both TCP and UDP usage.
*/
@CalledByNative
private static long getTotalRxBytes() {
long bytes = TrafficStats.getTotalRxBytes();
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
public static double getUidTxBytes(int uid) {
return TrafficStats.getUidTxBytes(uid) == TrafficStats.UNSUPPORTED ? 0 : TrafficStats.getUidTxBytes(uid);
}
private long getTotalRxBytes() {
return TrafficStats.getUidRxBytes(getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
}
private long getTotalRxBytes() {
return TrafficStats.getUidRxBytes(mContext.getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB
}
/**
* @return Number of bytes received since device boot. Counts packets across all network
* interfaces, and always increases monotonically since device boot. Statistics are
* measured at the network layer, so they include both TCP and UDP usage.
*/
@CalledByNative
private static long getTotalRxBytes() {
long bytes = TrafficStats.getTotalRxBytes();
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
/**
* @return Number of bytes transmitted since device boot that were attributed to caller's UID.
* Counts packets across all network interfaces, and always increases monotonically
* since device boot. Statistics are measured at the network layer, so they include
* both TCP and UDP usage.
*/
@CalledByNative
private static long getCurrentUidTxBytes() {
long bytes = TrafficStats.getUidTxBytes(Process.myUid());
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
/**
* @return Number of bytes received since device boot that were attributed to caller's UID.
* Counts packets across all network interfaces, and always increases monotonically
* since device boot. Statistics are measured at the network layer, so they include
* both TCP and UDP usage.
*/
@CalledByNative
private static long getCurrentUidRxBytes() {
long bytes = TrafficStats.getUidRxBytes(Process.myUid());
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
/**
* @return Number of bytes transmitted since device boot that were attributed to caller's UID.
* Counts packets across all network interfaces, and always increases monotonically
* since device boot. Statistics are measured at the network layer, so they include
* both TCP and UDP usage.
*/
@CalledByNative
private static long getCurrentUidTxBytes() {
long bytes = TrafficStats.getUidTxBytes(Process.myUid());
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}
/**
* @return Number of bytes received since device boot that were attributed to caller's UID.
* Counts packets across all network interfaces, and always increases monotonically
* since device boot. Statistics are measured at the network layer, so they include
* both TCP and UDP usage.
*/
@CalledByNative
private static long getCurrentUidRxBytes() {
long bytes = TrafficStats.getUidRxBytes(Process.myUid());
return bytes != TrafficStats.UNSUPPORTED ? bytes : TrafficStatsError.ERROR_NOT_SUPPORTED;
}