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

The following examples show how to use android.net.TrafficStats#getTotalRxBytes() . 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: DeviceInfoActivty.java    From PracticeDemo with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ISMService.java    From internet-speed-meter with MIT License 6 votes vote down vote up
private void getDownloadSpeed() {

        long mRxBytesPrevious = TrafficStats.getTotalRxBytes();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long mRxBytesCurrent = TrafficStats.getTotalRxBytes();

        long mDownloadSpeed = mRxBytesCurrent - mRxBytesPrevious;

        float mDownloadSpeedWithDecimals;

        if (mDownloadSpeed >= 1000000000) {
            mDownloadSpeedWithDecimals = (float) mDownloadSpeed / (float) 1000000000;
            mUnits = " GB";
        } else if (mDownloadSpeed >= 1000000) {
            mDownloadSpeedWithDecimals = (float) mDownloadSpeed / (float) 1000000;
            mUnits = " MB";

        } else {
            mDownloadSpeedWithDecimals = (float) mDownloadSpeed / (float) 1000;
            mUnits = " KB";
        }


        if (!mUnits.equals(" KB") && mDownloadSpeedWithDecimals < 100) {
            mDownloadSpeedOutput = String.format(Locale.US, "%.1f", mDownloadSpeedWithDecimals);
        } else {
            mDownloadSpeedOutput = Integer.toString((int) mDownloadSpeedWithDecimals);
        }

    }
 
Example 3
Source File: NetworkTools.java    From SoloPi with Apache License 2.0 6 votes vote down vote up
public static float getRxTotal()
{
	long currentTime = System.currentTimeMillis();
	long currentRx = TrafficStats.getTotalRxBytes();
	
	if (lastRxTime == 0 || lastRx == 0)
	{
		lastRxTime = currentTime;
		lastRx = currentRx;
		return 0;
	}
	
	float speed = (currentRx - lastRx) / (float)(currentTime - lastRxTime);
	lastRx = currentRx;
	lastRxTime = currentTime;
	
	return speed;
}
 
Example 4
Source File: TrafficStatsActivity.java    From MobileGuard with MIT License 6 votes vote down vote up
/**
 * 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)));

}
 
Example 5
Source File: SystemPlayerManager.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: main.java    From trafficstats-example with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example 7
Source File: main.java    From trafficstats-example with MIT License 5 votes vote down vote up
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);
}
 
Example 8
Source File: MyWindowManager.java    From AndroidNetworkSpeed with GNU General Public License v3.0 5 votes vote down vote up
public void initData() {
    mobileRecvSum = TrafficStats.getMobileRxBytes();
    mobileSendSum = TrafficStats.getMobileTxBytes();
    wlanRecvSum = TrafficStats.getTotalRxBytes() - mobileRecvSum;
    wlanSendSum = TrafficStats.getTotalTxBytes() - mobileSendSum;
    rxtxTotal = TrafficStats.getTotalRxBytes()
            + TrafficStats.getTotalTxBytes();
}
 
Example 9
Source File: TrafficServiceImpl.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@Override
public int start() {
	if ((trafficRxStart = TrafficStats.getTotalRxBytes()) == TrafficStats.UNSUPPORTED) {
		return SERVICE_NOT_SUPPORTED;
	}		
	running = true;
	trafficTxStart = TrafficStats.getTotalTxBytes();
	return SERVICE_START_OK;
}
 
Example 10
Source File: InterfaceTrafficGatherer.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public InterfaceTrafficGatherer() {
	prevTxBytes = TrafficStats.getTotalTxBytes();
	prevRxBytes = TrafficStats.getTotalRxBytes();
	txBytes = prevTxBytes;
	rxBytes = prevRxBytes;
	nsTimestamp = System.nanoTime();
}
 
Example 11
Source File: Device.java    From callmeter with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public long getWiFiRxBytes() throws IOException {
    final long l = TrafficStats.getMobileRxBytes();
    final long la = TrafficStats.getTotalRxBytes();
    if (la < 0L || la < l) {
        return 0L;
    }
    return la - l;
}
 
Example 12
Source File: TrafficServiceImpl.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
	if (running) {
		running = false;
		trafficTxEnd = TrafficStats.getTotalTxBytes();
		trafficRxEnd = TrafficStats.getTotalRxBytes();
	}
}
 
Example 13
Source File: WebsiteTestServiceImpl.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
private void setEndTrafficCounter() {
	if (USE_PROCESS_UID_FOR_TRAFFIC_MEASUREMENT) {
		this.trafficRxEnd = TrafficStats.getUidRxBytes(processUid);
		this.trafficTxEnd = TrafficStats.getUidTxBytes(processUid);			
	}
	else {
		this.trafficRxEnd = TrafficStats.getTotalRxBytes();
		this.trafficTxEnd = TrafficStats.getTotalTxBytes();			
	}
}
 
Example 14
Source File: AndroidTrafficStats.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @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;
}
 
Example 15
Source File: CustomMediaController.java    From MyHearts with Apache License 2.0 4 votes vote down vote up
private long getTotalRxBytes() {
    return TrafficStats.getUidRxBytes(mContext.getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB
}
 
Example 16
Source File: NetTrafficSpider.java    From NetUpDown with Apache License 2.0 4 votes vote down vote up
public void setStartTotalBytes(long startTotalBytes) {
    if (startTotalBytes < TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes()) {
        this.startTotalBytes = startTotalBytes;
    }
}
 
Example 17
Source File: MyWindowManager.java    From AndroidNetworkSpeed with GNU General Public License v3.0 4 votes vote down vote up
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));
        }

    }
 
Example 18
Source File: TrafficUtils.java    From YCWebView with Apache License 2.0 2 votes vote down vote up
/**
 * 获取当前下载流量总和
 *
 * @return
 */
public static long getNetworkRxBytes() {
    return TrafficStats.getTotalRxBytes();
}
 
Example 19
Source File: DeviceUtils.java    From PracticeDemo with Apache License 2.0 2 votes vote down vote up
public static long getTotalRxBytes(){

        return TrafficStats.getTotalRxBytes();
    }
 
Example 20
Source File: NETUtils.java    From GTTools with MIT License 2 votes vote down vote up
/**
 * 获取整体的网络接收流量,包括wifi和Mobile
 * 
 * @return 总字节数
 */
public static long getNetRxTotalBytes() {
	long total = TrafficStats.getTotalRxBytes();
	return total;
}