Java Code Examples for android.net.TrafficStats#UNSUPPORTED

The following examples show how to use android.net.TrafficStats#UNSUPPORTED . 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: 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 2
Source File: TrafficStatsNetworkBytesCollector.java    From Battery-Metrics with MIT License 6 votes vote down vote up
@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;
}
 
Example 3
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 4
Source File: Exo2PlayerManager.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 5
Source File: NetStatsDataModule.java    From DebugOverlay-Android with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 6
Source File: TrafficServiceByUidImpl.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: NetTrafficSpider.java    From NetUpDown with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
 
Example 8
Source File: ConnectionFragment.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
@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)");
        }
    }
}
 
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: NetTrafficSpider.java    From NetUpDown with Apache License 2.0 5 votes vote down vote up
public void reset() {
    startTotalBytes = 0;
    lastTotalBytes = TrafficStats.UNSUPPORTED;
    lastTotalTxBytes = TrafficStats.UNSUPPORTED;
    lastTotalRxBytes = TrafficStats.UNSUPPORTED;
    refreshPeriod = 1000;
    reqStop = false;
}
 
Example 11
Source File: TrafficSampler.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
public static double getUidTxBytes(int uid) {
    return TrafficStats.getUidTxBytes(uid) == TrafficStats.UNSUPPORTED ? 0 : TrafficStats.getUidTxBytes(uid);
}
 
Example 12
Source File: AndroidTrafficStats.java    From 365browser with Apache License 2.0 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 13
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 14
Source File: VideoViewActivity.java    From MyHearts with Apache License 2.0 4 votes vote down vote up
private long getTotalRxBytes() {
    return TrafficStats.getUidRxBytes(getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
}
 
Example 15
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 16
Source File: AndroidTrafficStats.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @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;
}
 
Example 17
Source File: AndroidTrafficStats.java    From cronet with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * @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;
}
 
Example 18
Source File: AndroidTrafficStats.java    From cronet with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * @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;
}
 
Example 19
Source File: AndroidTrafficStats.java    From 365browser with Apache License 2.0 2 votes vote down vote up
/**
 * @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;
}
 
Example 20
Source File: AndroidTrafficStats.java    From 365browser with Apache License 2.0 2 votes vote down vote up
/**
 * @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;
}