最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?獲取實時網(wǎng)速實現(xiàn)詳解

 更新時間:2022年11月27日 16:08:22   作者:ChenYhong  
這篇文章主要為大家介紹了Android?獲取實時網(wǎng)速實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

正文

最近接到個需求,需要計算WebView加載網(wǎng)頁時的網(wǎng)速。查詢了一下,Android沒有提供直接獲取網(wǎng)速的Api,但是提供了獲取流量的類TrafficStats。本文介紹如何使用Trafficstats來實現(xiàn)獲取網(wǎng)速功能。

TrafficStats簡介

TrafficStats提供了一些獲取設備從本次開機到目前為止傳輸/接收的流量的接口,如下:

方法參數(shù)說明
getTotalTxBytes-獲取設備本次開機到目前為止,WI-FI、流量下傳輸?shù)淖止?jié)總數(shù)。
getTotalRxBytes-獲取設備本次開機到目前為止,WI-FI、流量下接收的字節(jié)總數(shù)。
getMobileTxBytes-獲取設備本次開機到目前為止,流量下傳輸?shù)淖止?jié)總數(shù)。
getMobileRxBytes-獲取設備本次開機到目前為止,流量下接收的字節(jié)總數(shù)。
getUidTxBytesuid獲取應用從本次開機到目前為止,WI-FI、流量下傳輸?shù)淖止?jié)總數(shù)。
getUidRxBytesuid獲取應用從本次開機到目前為止,WI-FI、流量下接收的字節(jié)總數(shù)。

上述接口可以滿足實現(xiàn)計算網(wǎng)速的需求,TrafficStats類其他接口可以查看官方文檔

實現(xiàn)獲取網(wǎng)速

可以通過一段時間內(nèi)傳輸?shù)牧髁砍r間計算出上行網(wǎng)速,通過一段時間內(nèi)接收的流量除去時間計算出下行網(wǎng)速。

TrafficStats類的接口獲取的網(wǎng)速是從開機時就開始計算的,因此,要計算一段時間內(nèi)的流量需要在開始時獲取一次流量數(shù)據(jù),結(jié)束時獲取一次流量數(shù)據(jù),相減得出一段時間的實際流量。

實時網(wǎng)速

本文用getUidTxBytesgetUidRxBytes來演示,其他方法也是類似的,如下:

object NetSpeedUtils {
    var netSpeedCallback: NetSpeedCallback? = null
    private var timer: Timer? = null
    private var timerTask: TimerTask? = null
    private var lastTotalReceiveBytes: Long = 0
    private var lastTotalTransferBytes: Long = 0
    /**
     * 根據(jù)應用uid獲取設備啟動以來,該應用接收到的總字節(jié)數(shù)
     *
     * @param uid 應用的uid
     */
    fun getTotalReceiveBytes(): Long {
        var receiveBytes: Long = TrafficStats.UNSUPPORTED.toLong()
        ExampleApplication.exampleContext?.run {
            receiveBytes = TrafficStats.getUidRxBytes(applicationInfo.uid)
        }
        // 當獲取不到時,會返回TrafficStats.UNSUPPORTED
        return if (receiveBytes == TrafficStats.UNSUPPORTED.toLong()) 0 else receiveBytes / 1024
    }
    /**
     * 根據(jù)應用uid獲取設備啟動以來,該應用傳輸?shù)目傋止?jié)數(shù)
     *
     * @param uid 應用的uid
     */
    fun getTotalTransferBytes(): Long {
        var transferBytes: Long = TrafficStats.UNSUPPORTED.toLong()
        ExampleApplication.exampleContext?.run {
            transferBytes = TrafficStats.getUidTxBytes(applicationInfo.uid)
        }
        // 當獲取不到時,會返回TrafficStats.UNSUPPORTED
        return if (transferBytes == TrafficStats.UNSUPPORTED.toLong()) 0 else transferBytes / 1024
    }
    // 通過Timer每隔1秒計算網(wǎng)速
    private fun calculateNetSpeed() {
        ExampleApplication.exampleContext?.run {
            val nowTotalReceiveBytes = getTotalReceiveBytes()
            val nowTotalTransferBytes = getTotalTransferBytes()
            val downloadSpeed = nowTotalReceiveBytes - lastTotalReceiveBytes
            val uploadSpeed = nowTotalTransferBytes - lastTotalTransferBytes
            lastTotalReceiveBytes = nowTotalReceiveBytes
            lastTotalTransferBytes = nowTotalTransferBytes
            netSpeedCallback?.onNetSpeedChange("$downloadSpeed kb/s", "$uploadSpeed kb/s")
        }
    }
    fun startMeasuringNetSpeed() {
        if (timer == null && timerTask == null) {
            timer = Timer()
            timerTask = object : TimerTask() {
                override fun run() {
                    calculateNetSpeed()
                }
            }
            timer?.run { timerTask?.let { schedule(it, 0L, 1000L) } }
        }
    }
    fun stopMeasuringNetSpeed() {
        timerTask?.cancel()
        timerTask = null
        timer?.cancel()
        timer = null
    }
    interface NetSpeedCallback {
        fun onNetSpeedChange(downloadSpeed: String, uploadSpeed: String)
    }
}
// 示例類
class TrafficStatsActivity : BaseGestureDetectorActivity() {
    private lateinit var binding: LayoutTrafficStatsActivityBinding
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.layout_traffic_stats_activity)
        binding.includeTitle.tvTitle.text = "TrafficStatsExample"
        NetSpeedUtils.netSpeedCallback = object : NetSpeedUtils.NetSpeedCallback {
            override fun onNetSpeedChange(downloadSpeed: String, uploadSpeed: String) {
                binding.tvNetSpeed.run { post { text = "downloadSpeed:$downloadSpeed , uploadSpeed:$uploadSpeed" } }
            }
        }
        binding.btnStartMeasureNetSpeed.setOnClickListener {
            NetSpeedUtils.startMeasuringNetSpeed()
        }
        binding.btnStopMeasureNetSpeed.setOnClickListener {
            NetSpeedUtils.stopMeasuringNetSpeed()
        }
        initWebViewSetting(binding.webView)
        binding.webView.loadUrl("https://go.minigame.vip/")
    }
    @SuppressLint("JavascriptInterface", "SetJavaScriptEnabled")
    private fun initWebViewSetting(webView: WebView?) {
        webView?.run {
            settings.cacheMode = WebSettings.LOAD_DEFAULT
            settings.domStorageEnabled = true
            settings.allowContentAccess = true
            settings.allowFileAccess = true
            settings.useWideViewPort = true
            settings.loadWithOverviewMode = true
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            settings.javaScriptEnabled = true
            settings.javaScriptCanOpenWindowsAutomatically = true
            settings.setSupportMultipleWindows(true)
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        binding.webView.clearHistory()
        binding.webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
        binding.root.run {
            if (this is ViewGroup) {
                this.removeView(binding.webView)
            }
        }
        binding.webView.destroy()
    }
}

效果如圖:

以上就是Android 獲取實時網(wǎng)速實現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于Android 獲取實時網(wǎng)速的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

仪陇县| 城固县| 大洼县| 长葛市| 舒城县| 秀山| 榆社县| 延安市| 常州市| 尼勒克县| 台湾省| 明星| 汶上县| 沂水县| 舞钢市| 宜黄县| 尉犁县| 惠来县| 柳河县| 射洪县| 蒙城县| 岳普湖县| 平陆县| 孟连| 荥经县| 西华县| 巴塘县| 邹城市| 信宜市| 定陶县| 苏尼特左旗| 北安市| 双峰县| 兴化市| 永州市| 普兰县| 冕宁县| 黔西| 桓台县| 衡山县| 九龙坡区|