下面列出了android.net.TrafficStats#getMobileTxBytes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* init data
*/
@Override
protected void initData() {
long totalRxBytes = TrafficStats.getTotalRxBytes();
long totalTxBytes = TrafficStats.getTotalTxBytes();
long mobileRxBytes = TrafficStats.getMobileRxBytes();
long mobileTxBytes = TrafficStats.getMobileTxBytes();
long totalBytes = totalRxBytes + totalTxBytes;
long mobileBytes = mobileRxBytes + mobileTxBytes;
tvTotalTrafficStatsSum.setText(getString(R.string.total_traffic_stats_sum, Formatter.formatFileSize(this, totalBytes)));
tvMobileTrafficStatsSum.setText(getString(R.string.mobile_traffic_stats_sum, Formatter.formatFileSize(this, mobileBytes)));
tvTotalTrafficStats.setText(getString(R.string.traffic_stats_upload_download, Formatter.formatFileSize(this, totalTxBytes), Formatter.formatFileSize(this, totalRxBytes)));
tvMobileTrafficStats.setText(getString(R.string.traffic_stats_upload_download, Formatter.formatFileSize(this, mobileTxBytes), Formatter.formatFileSize(this, mobileRxBytes)));
}
@Override
public boolean handleMessage(Message msg) {
//不准?...
long mrx = TrafficStats.getMobileRxBytes() / 1024; ////获取通过Mobile连接收到的字节总数,不包含WiFi
long mtx = TrafficStats.getMobileTxBytes() / 1024; //Mobile发送的总字节数
long trx = (long) ((TrafficStats.getTotalRxBytes() - mTotalRxBytes) * 1.00f / 1024);
mTotalRxBytes = TrafficStats.getTotalRxBytes(); //获取总的接受字节数,包含Mobile和WiFi等
long ttx = TrafficStats.getTotalTxBytes() / 1024; //总的发送字节数,包含Mobile和WiFi等
long uidrx = TrafficStats.getUidRxBytes(getApplicationInfo().uid) / 1024;//获取某个网络UID的接受字节数,某一个进程的总接收量
long uidtx = TrafficStats.getUidTxBytes(getApplicationInfo().uid) / 1024;//获取某个网络UID的发送字节数,某一个进程的总发送量
StringBuilder sb = new StringBuilder();
sb.append("mrx:" + mrx + "\n\r")
.append("mtx:" + mtx + "\n\r")
.append("trx:" + trx + "\n\r")
.append("ttx:" + ttx + "\n\r")
.append("uidrx:" + uidrx + "\n\r")
.append("uidtx:" + uidtx + "\n\r")
;
mTvDeviceInfo.setText(sb.toString());
mHandler.sendEmptyMessageDelayed(0, 1000);
return true;
}
private void scheduleAlarm() {
Intent intent = new Intent(ACTION_CHANGE_MODE_ALARM);
mPendingIntent = PendingIntent.getBroadcast(mContext, 1, intent, PendingIntent.FLAG_ONE_SHOT);
long triggerAtMillis = System.currentTimeMillis() + mScreenOffDelay*60*1000;
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, mPendingIntent);
mLinkActivity.timestamp = System.currentTimeMillis();
mLinkActivity.rxBytes = TrafficStats.getMobileRxBytes();
mLinkActivity.txBytes = TrafficStats.getMobileTxBytes();
}
private boolean shouldPostponeAlarm() {
boolean postpone = false;
if (mAdaptiveDelayThreshold > 0) {
// if there's link activity higher than defined threshold
long rxDelta = TrafficStats.getMobileRxBytes() - mLinkActivity.rxBytes;
long txDelta = TrafficStats.getMobileTxBytes() - mLinkActivity.txBytes;
long timeDelta = System.currentTimeMillis() - mLinkActivity.timestamp;
long speedRxKBs = (long)(rxDelta / (timeDelta / 1000f)) / 1024;
long speedTxKBs = (long)(txDelta / (timeDelta / 1000f)) / 1024;
postpone |= speedTxKBs >= mAdaptiveDelayThreshold || speedRxKBs >= mAdaptiveDelayThreshold;
if (DEBUG) log("shouldPostponeAlarm: speedRxKBs=" + speedRxKBs +
"; speedTxKBs=" + speedTxKBs + "; threshold=" + mAdaptiveDelayThreshold);
}
return postpone;
}
public void initData() {
mobileRecvSum = TrafficStats.getMobileRxBytes();
mobileSendSum = TrafficStats.getMobileTxBytes();
wlanRecvSum = TrafficStats.getTotalRxBytes() - mobileRecvSum;
wlanSendSum = TrafficStats.getTotalTxBytes() - mobileSendSum;
rxtxTotal = TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
}
public void run() {
long mobile = TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes();
long total = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();
tvDataUsageWiFi.setText("" + (total - mobile) / 1024 + " Kb");
tvDataUsageMobile.setText("" + mobile / 1024 + " Kb");
tvDataUsageTotal.setText("" + total / 1024 + " Kb");
if (dataUsageTotalLast != total) {
dataUsageTotalLast = total;
updateAdapter();
}
handler.postDelayed(runnable, 5000);
}
/**
* Determine how many packets, so far, have been sent (the contents of /proc/net/dev/). This is
* a global value. We use this to determine if any other app anywhere on the phone may have sent
* interfering traffic that might have changed the RRC state without our knowledge.
*
* @return Two values: number of bytes or packets received at index 0, followed by the number
* sent at index 1.
*/
public long[] getPacketsSent() {
long[] retval = {-1, -1};
if (bySize) {
retval[0] = TrafficStats.getMobileRxBytes();
retval[1] = TrafficStats.getMobileTxBytes();
} else {
retval[0] = TrafficStats.getMobileRxPackets();
retval[1] = TrafficStats.getMobileTxPackets();
}
return retval;
}
/**
* {@inheritDoc}
*/
@Override
public long getCellTxBytes() throws IOException {
final long l = TrafficStats.getMobileTxBytes();
if (l < 0L) {
return 0L;
}
return l;
}
/**
* {@inheritDoc}
*/
@Override
public long getWiFiTxBytes() throws IOException {
final long l = TrafficStats.getMobileTxBytes();
final long la = TrafficStats.getTotalTxBytes();
if (la < 0L || la < l) {
return 0L;
}
return la - l;
}
public void updateViewData() {
long tempSum = TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
long rxtxLast = tempSum - rxtxTotal;
double totalSpeed = rxtxLast * 1000 / TIME_SPAN;
rxtxTotal = tempSum;
long tempMobileRx = TrafficStats.getMobileRxBytes();
long tempMobileTx = TrafficStats.getMobileTxBytes();
long tempWlanRx = TrafficStats.getTotalRxBytes() - tempMobileRx;
long tempWlanTx = TrafficStats.getTotalTxBytes() - tempMobileTx;
long mobileLastRecv = tempMobileRx - mobileRecvSum;
long mobileLastSend = tempMobileTx - mobileSendSum;
long wlanLastRecv = tempWlanRx - wlanRecvSum;
long wlanLastSend = tempWlanTx - wlanSendSum;
double mobileRecvSpeed = mobileLastRecv * 1000 / TIME_SPAN;
double mobileSendSpeed = mobileLastSend * 1000 / TIME_SPAN;
double wlanRecvSpeed = wlanLastRecv * 1000 / TIME_SPAN;
double wlanSendSpeed = wlanLastSend * 1000 / TIME_SPAN;
mobileRecvSum = tempMobileRx;
mobileSendSum = tempMobileTx;
wlanRecvSum = tempWlanRx;
wlanSendSum = tempWlanTx;
//==========================================================
if (isMulti) {
if (mobileRecvSpeed >= 0d) {
tvMobileRx.setText(showSpeed(mobileRecvSpeed));
}
if (mobileSendSpeed >= 0d) {
tvMobileTx.setText(showSpeed(mobileSendSpeed));
}
if (wlanRecvSpeed >= 0d) {
tvWlanRx.setText(showSpeed(wlanRecvSpeed));
}
if (wlanSendSpeed >= 0d) {
tvWlanTx.setText(showSpeed(wlanSendSpeed));
}
} else {
//==============================================================
if (totalSpeed >= 0d) {
tvSum.setText(showSpeed(totalSpeed));
}
}
//==============================================================
}
public static long getNetTxMobileBytes() {
long total = TrafficStats.getMobileTxBytes();
return total;
}
public void updateViewData(Context context) {
long tempSum = TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
long rxtxLast = tempSum - rxtxTotal;
double totalSpeed = rxtxLast * 1000 / TIME_SPAN;
rxtxTotal = tempSum;
long tempMobileRx = TrafficStats.getMobileRxBytes();
long tempMobileTx = TrafficStats.getMobileTxBytes();
long tempWlanRx = TrafficStats.getTotalRxBytes() - tempMobileRx;
long tempWlanTx = TrafficStats.getTotalTxBytes() - tempMobileTx;
long mobileLastRecv = tempMobileRx - mobileRecvSum;
long mobileLastSend = tempMobileTx - mobileSendSum;
long wlanLastRecv = tempWlanRx - wlanRecvSum;
long wlanLastSend = tempWlanTx - wlanSendSum;
double mobileRecvSpeed = mobileLastRecv * 1000 / TIME_SPAN;
double mobileSendSpeed = mobileLastSend * 1000 / TIME_SPAN;
double wlanRecvSpeed = wlanLastRecv * 1000 / TIME_SPAN;
double wlanSendSpeed = wlanLastSend * 1000 / TIME_SPAN;
mobileRecvSum = tempMobileRx;
mobileSendSum = tempMobileTx;
wlanRecvSum = tempWlanRx;
wlanSendSum = tempWlanTx;
if (mBigWindowView != null) {
if (mobileRecvSpeed >= 0d) {
tvMobileRx.setText(showSpeed(mobileRecvSpeed));
}
if (mobileSendSpeed >= 0d) {
tvMobileTx.setText(showSpeed(mobileSendSpeed));
}
if (wlanRecvSpeed >= 0d) {
tvWlanRx.setText(showSpeed(wlanRecvSpeed));
}
if (wlanSendSpeed >= 0d) {
tvWlanTx.setText(showSpeed(wlanSendSpeed));
}
}
if (mSmallWindowView != null && totalSpeed >= 0d) {
tvSum.setText(showSpeed(totalSpeed));
}
}