android.net.TrafficStats#UNSUPPORTED源码实例Demo

下面列出了android.net.TrafficStats#UNSUPPORTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: DebugOverlay-Android   文件: NetStatsDataModule.java
@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;
}
 
源代码3 项目: GSYVideoPlayer   文件: SystemPlayerManager.java
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;
}
 
源代码4 项目: GSYVideoPlayer   文件: Exo2PlayerManager.java
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;
}
 
源代码5 项目: trafficstats-example   文件: main.java
@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);
    }
}
 
源代码6 项目: Android-Remote   文件: ConnectionFragment.java
@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)");
        }
    }
}
 
源代码7 项目: open-rmbt   文件: TrafficServiceImpl.java
@Override
public int start() {
	if ((trafficRxStart = TrafficStats.getTotalRxBytes()) == TrafficStats.UNSUPPORTED) {
		return SERVICE_NOT_SUPPORTED;
	}		
	running = true;
	trafficTxStart = TrafficStats.getTotalTxBytes();
	return SERVICE_START_OK;
}
 
源代码8 项目: NetUpDown   文件: NetTrafficSpider.java
public void reset() {
    startTotalBytes = 0;
    lastTotalBytes = TrafficStats.UNSUPPORTED;
    lastTotalTxBytes = TrafficStats.UNSUPPORTED;
    lastTotalRxBytes = TrafficStats.UNSUPPORTED;
    refreshPeriod = 1000;
    reqStop = false;
}
 
源代码9 项目: NetUpDown   文件: NetTrafficSpider.java
@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);
            }
        }
 
源代码10 项目: open-rmbt   文件: TrafficServiceByUidImpl.java
@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;
}
 
源代码11 项目: cronet   文件: AndroidTrafficStats.java
/**
 * @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;
}
 
源代码12 项目: cronet   文件: AndroidTrafficStats.java
/**
 * @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);
}
 
源代码14 项目: MyHearts   文件: VideoViewActivity.java
private long getTotalRxBytes() {
    return TrafficStats.getUidRxBytes(getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
}
 
源代码15 项目: MyHearts   文件: CustomMediaController.java
private long getTotalRxBytes() {
    return TrafficStats.getUidRxBytes(mContext.getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB
}
 
源代码16 项目: 365browser   文件: AndroidTrafficStats.java
/**
 * @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;
}
 
源代码17 项目: cronet   文件: AndroidTrafficStats.java
/**
 * @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;
}
 
源代码18 项目: cronet   文件: AndroidTrafficStats.java
/**
 * @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;
}
 
源代码19 项目: 365browser   文件: AndroidTrafficStats.java
/**
 * @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;
}
 
源代码20 项目: 365browser   文件: AndroidTrafficStats.java
/**
 * @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;
}