I don't mind resetting the TrafficStats value before launching the application. For this I use TrafficStats.clearThreadStatsTag(), but TrafficStats does not clear. I know that TrafficStats is being reset. happens when the phone is rebooted It is possible to reset TrafficStats. via code
fun myNetvorkCheck(){
checkNetworkConnection = CheckNetworkConnection(application)
checkNetworkConnection.observe(this, { isConnected->
if (isConnected) {
checkTraficStart()
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show()
}else{
checkTrafficEnd()
Toast.makeText(this, "No Connected",Toast.LENGTH_SHORT).show()
}
})
}
fun checkTraficStart() {
//resetMobile()
//total dowlaood bytes
val dowladBytes = TrafficStats.getTotalRxBytes()
//total uload bytes
val uploaBytes = TrafficStats.getTotalTxBytes()
// only star mobile dowlaod bytes
val startdowMobile = TrafficStats.getMobileRxBytes()
//only start mobile upload bytes
val startuploMobile = TrafficStats.getMobileTxBytes()
//convert bytes to Mb
val finalmobDowload = ( startdowMobile ) / 1048576
val finalmodUpload1 = ( startuploMobile ) / 1048576
//total traffic za ves den
val modileZaden = finalmobDowload + finalmodUpload1
startmobileTraffic = modileZaden.toInt()
textDowload.text = finalmobDowload.toInt().toString()
textUpload.text = finalmodUpload1.toInt().toString()
txtInfo.text = modileZaden.toInt().toString()
}
fun checkTrafficEnd() {
endmobileTraffic = endmobileTraffic + startmobileTraffic
mobileVse = startmobileTraffic
textObchiy.text = endmobileTraffic.toString()
saveTrafic(mobileVse)
saveTrafficAll(endmobileTraffic)
resetMobile()
}
fun saveTrafic(res:Int) {
val edit = traficPref?.edit()
edit?.putInt("key",res)
edit?.apply()
}
fun saveTrafficAll(res: Int){
val alledit = traficPrefAll?.edit()
alledit?.putInt("keyAll",res)
alledit?.apply()
}
fun resetMobile() {
TrafficStats.clearThreadStatsTag()
}
why textDowload.text = 515 textUpload.text = 50 and not textDowload.text = 0 textUpload.text = 0 ????