當前位置: 首頁>>代碼示例>>Java>>正文


Java TrafficStats類代碼示例

本文整理匯總了Java中android.net.TrafficStats的典型用法代碼示例。如果您正苦於以下問題:Java TrafficStats類的具體用法?Java TrafficStats怎麽用?Java TrafficStats使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


TrafficStats類屬於android.net包,在下文中一共展示了TrafficStats類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getNetSpeed

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * 得到網絡速度
 *
 * @param context
 * @return
 */
public String getNetSpeed(Context context) {
    long lastTotalRxBytes = 0;
    long lastTimeStamp = 0;

    String netSpeed = "0 kb/s";
    long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) ==
            TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//轉為KB;

    long nowTimeStamp = System.currentTimeMillis();

    long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒轉換

    lastTimeStamp = nowTimeStamp;
    lastTotalRxBytes = nowTotalRxBytes;
    netSpeed = String.valueOf(speed) + " kb/s";

    return netSpeed;
}
 
開發者ID:hushengjun,項目名稱:FastAndroid,代碼行數:25,代碼來源:BaseTool.java

示例2: onCreate

import android.net.TrafficStats; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_traffic);
	
	//��ȡ�ֻ���������
	//��ȡ����(R �ֻ�(2G,3G,4G)��������)
	long mobileRxBytes = TrafficStats.getMobileRxBytes();
	//��ȡ�ֻ���������(�ϴ�+����)
	//T total(�ֻ�(2G,3G,4G)������(�ϴ�+����))
	long mobileTxBytes = TrafficStats.getMobileTxBytes();
	//total(���������ܺ�(�ֻ�+wifi))
	long totalRxBytes = TrafficStats.getTotalRxBytes();
	//(������(�ֻ�+wifi),(�ϴ�+����))
	long totalTxBytes = TrafficStats.getTotalTxBytes();
	
	//���岻��
	//������ȡģ��(���Ͷ���),��Ӫ��(��ͨ,�ƶ�....),(��������)�������ӿ�,���
	//����ע��
	
	
	
}
 
開發者ID:cckevincyh,項目名稱:mobilesafe,代碼行數:24,代碼來源:TrafficActivity.java

示例3: run

import android.net.TrafficStats; //導入依賴的package包/類
@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);
}
 
開發者ID:Manabu-GT,項目名稱:DebugOverlay-Android,代碼行數:22,代碼來源:NetStatsDataModule.java

示例4: createTransferStatSection

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * Creates a section showing some stats from {@link TrafficStats} showing tx/rx bytes and packages
 * of tagged sockets from boot until now.
 *
 * @param uids optional uid to get rx/tx bytes and packages
 * @return section
 */
public static Section.HeaderSection createTransferStatSection(int... uids) {
    Section.ModifiableHeaderSection section = Hood.ext().createSection("Traffic Stats");

    if (uids != null) {
        for (int uid : uids) {
            section.add(createTxRxdSection("Socket " + uid, TrafficStats.getUidTxBytes(uid), TrafficStats.getUidTxPackets(uid),
                    TrafficStats.getUidRxBytes(uid), TrafficStats.getUidRxPackets(uid)));
        }
    }
    section.add(createTxRxdSection("Mobile", TrafficStats.getMobileTxBytes(), TrafficStats.getMobileTxPackets(),
            TrafficStats.getMobileRxBytes(), TrafficStats.getMobileRxPackets()));
    section.add(createTxRxdSection("Total", TrafficStats.getTotalTxBytes(), TrafficStats.getTotalTxPackets(),
            TrafficStats.getTotalRxBytes(), TrafficStats.getTotalRxPackets()));

    return section;
}
 
開發者ID:patrickfav,項目名稱:under-the-hood,代碼行數:24,代碼來源:DefaultProperties.java

示例5: createTxRxdSection

import android.net.TrafficStats; //導入依賴的package包/類
private static List<PageEntry<?>> createTxRxdSection(String name, final long txBytes,
                                                     final long txPackets, final long rxBytes, final long rxPackets) {
    List<PageEntry<?>> list = new LinkedList<>();
    list.add(Hood.get().createPropertyEntry(name + " TX", new DynamicValue<String>() {
        @Override
        public String getValue() {
            return txBytes == TrafficStats.UNSUPPORTED ? "UNSUPPORTED" : txPackets + " pkt / " + HoodUtil.humanReadableByteCount(txBytes, false);
        }
    }));
    list.add(Hood.get().createPropertyEntry(name + " RCVD", new DynamicValue<String>() {
        @Override
        public String getValue() {
            return rxPackets == TrafficStats.UNSUPPORTED ? "UNSUPPORTED" : rxPackets + " pkt / " + HoodUtil.humanReadableByteCount(rxBytes, false);
        }
    }));
    return list;
}
 
開發者ID:patrickfav,項目名稱:under-the-hood,代碼行數:18,代碼來源:DefaultProperties.java

示例6: initData

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * 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)));

}
 
開發者ID:ittianyu,項目名稱:MobileGuard,代碼行數:20,代碼來源:TrafficStatsActivity.java

示例7: dataThrougput

import android.net.TrafficStats; //導入依賴的package包/類
public void dataThrougput() {
	synchronized(this) {
		totalRxBytes = TrafficStats.getTotalRxBytes();		
		totalTxBytes = TrafficStats.getTotalTxBytes();		
		if (dataActivityRunnable.hasDataActivity == 0) {
			//dataActivityRunnable.initializeHasDataActivity(1);
			dataActivityRunnable.init(totalRxBytes, totalTxBytes, true);
		}	
		else if (dataActivityRunnable.hasDataActivity == 1)
		{
			//MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "onDataActivity", "in sampling");
		}
		else if (dataActivityRunnable.hasDataActivity == 2)
		{
			//MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "onDataActivity", "already in download");
		}
	}
}
 
開發者ID:RestComm,項目名稱:android-QoS,代碼行數:19,代碼來源:LibPhoneStateListener.java

示例8: updateSignalStrength

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * This method sends the signal strength update to all listeners of the signal strength update action.
 * This update includes the absolute signal strength (in dbm) and the percentage it has been assigned.
 * @param signalStrength
 */
public void updateSignalStrength(int signalStrength, int nettype, boolean bWifiConnected, int wifiSignal){
	Intent intent = new Intent(CommonIntentBundleKeysOld.ACTION_SIGNAL_STRENGTH_UPDATE);
	intent.putExtra(CommonIntentBundleKeysOld.KEY_UPDATE_SIGNAL_STRENGTH_DBM, signalStrength);
	intent.putExtra(CommonIntentBundleKeysOld.KEY_UPDATE_NETTYPE, nettype);

	//if (owner.wsConnected) {
		long totalRxBytes = TrafficStats.getTotalRxBytes();
		long totalTxBytes = TrafficStats.getTotalTxBytes();
		intent.putExtra(CommonIntentBundleKeysOld.KEY_RX, totalRxBytes);
		intent.putExtra(CommonIntentBundleKeysOld.KEY_TX, totalTxBytes);
	intent.putExtra(CommonIntentBundleKeysOld.KEY_WIFI_CONNECTED, bWifiConnected);
	intent.putExtra(CommonIntentBundleKeysOld.KEY_WIFI_SIGNAL, wifiSignal);


	//}

	owner.sendBroadcast(intent);
}
 
開發者ID:RestComm,項目名稱:android-QoS,代碼行數:24,代碼來源:IntentDispatcher.java

示例9: updateRxTx

import android.net.TrafficStats; //導入依賴的package包/類
public void updateRxTx ()
{
	if (lastRxTxUpdate + 4000 < System.currentTimeMillis())
	{
		// Update Neighbor list history if state changes
		long rx = TrafficStats.getTotalRxBytes();
		long tx = TrafficStats.getTotalTxBytes();
		tcpstats.readTcpStats(false);
		tcpstats.updateCounts();
		int tcprsts = tcpstats.tcpResets;
		int tcperrs = tcpstats.tcpErrors;
		int tcpretrans = tcpstats.tcpRetrans;

		if (rx > rxLast + 10000 || tx > txLast + 10000 || tcpstats.tcpResets > tcpstats.prevResets || tcpstats.tcpErrors > tcpstats.prevErrors || tcpstats.tcpRetrans > tcpstats.prevRetrans) {
			ConnectionSample smp2 = new ConnectionSample(TYPE_RXTX, rx, tx, tcprsts, tcperrs, tcpretrans);
			rxLast = rx; txLast = tx;
			connect_history.add(smp2);
		}
		lastRxTxUpdate = System.currentTimeMillis();
	}
}
 
開發者ID:RestComm,項目名稱:android-QoS,代碼行數:22,代碼來源:ConnectionHistory.java

示例10: handleMessage

import android.net.TrafficStats; //導入依賴的package包/類
@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;
}
 
開發者ID:AlanCheen,項目名稱:PracticeDemo,代碼行數:27,代碼來源:DeviceInfoActivty.java

示例11: close

import android.net.TrafficStats; //導入依賴的package包/類
public final void close()
  throws IOException
{
  try
  {
    super.close();
    long l4;
    long l5;
    long l6;
    Object[] arrayOfObject2;
    return;
  }
  finally
  {
    long l1 = SystemClock.elapsedRealtime() - NetworkStatsEntity.this.mProcessingStartTime;
    long l2 = TrafficStats.getUidTxBytes(NetworkStatsEntity.this.mUid);
    long l3 = TrafficStats.getUidRxBytes(NetworkStatsEntity.this.mUid);
    Object[] arrayOfObject1 = new Object[5];
    arrayOfObject1[0] = NetworkStatsEntity.this.mUa;
    arrayOfObject1[1] = Long.valueOf(NetworkStatsEntity.this.mResponseLatency);
    arrayOfObject1[2] = Long.valueOf(l1);
    arrayOfObject1[3] = Long.valueOf(l2 - NetworkStatsEntity.this.mStartTx);
    arrayOfObject1[4] = Long.valueOf(l3 - NetworkStatsEntity.this.mStartRx);
    EventLog.writeEvent(52001, arrayOfObject1);
  }
}
 
開發者ID:ChiangC,項目名稱:FMTech,代碼行數:27,代碼來源:NetworkStatsEntity.java

示例12: getTraffic

import android.net.TrafficStats; //導入依賴的package包/類
public static HashMap<String, Long> getTraffic() {
    HashMap<String, Long> hashMap = new HashMap<>();

    long mobileRxBytes = TrafficStats.getMobileRxBytes();
    long mobileTxBytes = TrafficStats.getMobileTxBytes();
    long totalRxBytes = TrafficStats.getTotalRxBytes();
    long totalTxbytes = TrafficStats.getTotalTxBytes();

    long wifiRxbytes = totalRxBytes - mobileRxBytes;
    long wifiTxbytes = totalTxbytes - mobileTxBytes;

    // total traffic
    hashMap.put("totRxB", totalRxBytes);
    hashMap.put("totTxB", totalTxbytes);

    // mobile traffic
    hashMap.put("mobRxB", mobileRxBytes);
    hashMap.put("mobTxB", mobileTxBytes);

    return hashMap;
}
 
開發者ID:ddinsight,項目名稱:dd-collector,代碼行數:22,代碼來源:Utils.java

示例13: addSample

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * Method for polling for the change in total bytes since last update and
 * adding it to the BandwidthManager.
 */
protected void addSample() {
    long newBytes = -1;
    newBytes = TrafficStats.getUidRxBytes(Process.myUid());
    if (newBytes == TrafficStats.UNSUPPORTED) {
        newBytes = TrafficStats.getTotalRxBytes();
    }
    long byteDiff = newBytes - sPreviousBytes;
    if (sPreviousBytes >= 0) {
        synchronized (this) {
            long curTimeReading = SystemClock.elapsedRealtime();
            mConnectionClassManager.addBandwidth(byteDiff, curTimeReading - mLastTimeReading);

            mLastTimeReading = curTimeReading;
        }
    }
    sPreviousBytes = newBytes;
}
 
開發者ID:WeiDianzhao1989,項目名稱:AndroidAppLib,代碼行數:22,代碼來源:DeviceBandwidthSampler.java

示例14: onCreate

import android.net.TrafficStats; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
    mStartRX = TrafficStats.getUidRxBytes(mUid);
    mStartTX = TrafficStats.getUidTxBytes(mUid);
    if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle("Uh Oh!");
        alert.setMessage("Your device does not support traffic stat monitoring.");
        alert.show();
        return;
    }
    mUid = android.os.Process.myUid();
}
 
開發者ID:legendmohe,項目名稱:LEHomeMobile_android,代碼行數:17,代碼來源:HomeStateFragment.java

示例15: getMobleTraffic

import android.net.TrafficStats; //導入依賴的package包/類
/**
 * 獲取應用使用的流量
 * 
 * @return
 */
private String getMobleTraffic() {
	long mobleTraffic = 0L;
	int uid = getAppUid();
	if (uid == -1) {
		return "0.00M";
	} else {
		mobleTraffic = (TrafficStats.getUidRxBytes(uid) == TrafficStats.UNSUPPORTED) ? 0
				: TrafficStats.getUidRxBytes(uid);
		mobleTraffic += (TrafficStats.getUidTxBytes(uid) == TrafficStats.UNSUPPORTED) ? 0
				: TrafficStats.getUidTxBytes(uid);
		String traffic = mobleTraffic / (float) (1024 * 1024) + "M";
		return traffic.substring(0, traffic.indexOf(".") + 3) + "M";
	}

}
 
開發者ID:justingboy,項目名稱:CouldBooks,代碼行數:21,代碼來源:ReadstatisticsActivity.java


注:本文中的android.net.TrafficStats類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。