Example usage for android.net TrafficStats getMobileTxBytes

List of usage examples for android.net TrafficStats getMobileTxBytes

Introduction

In this page you can find the example usage for android.net TrafficStats getMobileTxBytes.

Prototype

public static long getMobileTxBytes() 

Source Link

Document

Return number of bytes transmitted across mobile networks since device boot.

Usage

From source file:Main.java

public static long getNetTxMobileBytes() {
    long total = TrafficStats.getMobileTxBytes();
    return total;
}

From source file:com.ultrafunk.network_info.receiver.MobileDataStatusReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //   Log.e(this.getClass().getSimpleName(), "onReceive(): " + action);

    updateMobileDataViews = true;/*from   ww w . j  a v a  2s.c  o m*/

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    dataState = telephonyManager.getDataState();
    isMobileDataEnabled = MobileDataUtils.isMobileDataEnabled(context);
    isAirplaneModeOn = MobileDataUtils.isAirplaneModeOn(context);
    isMobileOutOfService = NetworkStateService.isMobileOutOfService();
    isDataRoaming = isDataRoaming(context);
    networkOperatorAndServiceProvider = getNetworkOperatorAndServiceProvider(context);
    dataUsageBytes = NetworkStateService
            .setGetDataUsageBytes(TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes());

    if (Constants.ACTION_DATA_CONNECTION_CHANGED.equals(action)
            || Constants.ACTION_DATA_STATE_CHANGED.equals(action)
            || Constants.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
        // Needed to get around a known bug in Android 5.x: https://code.google.com/p/android/issues/detail?id=78924
        if ((dataState == TelephonyManager.DATA_CONNECTED) && (dataUsageBytes == 0)
                && !NetworkStateService.isWaitingForDataUsage()) {
            NetworkStateService.setWaitingForDataUsage(true);
            Intent serviceIntent = new Intent(context, NetworkStateService.class);
            serviceIntent.setAction(Constants.ACTION_DATA_CONNECTED);
            context.startService(serviceIntent);
        }

        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_DATA_USAGE_UPDATE.equals(action) || Intent.ACTION_SCREEN_ON.equals(action)) {
        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_UPDATE_WIDGET.equals(action)) {
        partiallyUpdateWidget(context, AppWidgetManager.getInstance(context),
                intent.getIntExtra(Constants.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
    }
}

From source file:pandroid.agent.PandroidAgentListener.java

/**
 *  Retrieves the number of sent/received bytes using the mobile network
 *//*from  w  w w .j  a v a  2  s. c  o  m*/
private void getDataBytes() {

    long receiveBytes = TrafficStats.getMobileRxBytes();
    long transmitBytes = TrafficStats.getMobileTxBytes();

    if (receiveBytes != TrafficStats.UNSUPPORTED && transmitBytes != TrafficStats.UNSUPPORTED) {
        putSharedData("PANDROID_DATA", "receiveBytes", "" + receiveBytes, "long");
        putSharedData("PANDROID_DATA", "transmitBytes", "" + transmitBytes, "long");
    }
}