How do you know how much traffic your mobile phone uses? After Android, we provided the trafficstats class to solve this problem. I will share with you the code below:
Package com. zyb. flow;
Import Android. App. activity;
Import android.net. trafficstats;
Import Android. OS. Bundle;
Public class mainactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Total traffic consumed by mobile phones, in MB
If (trafficstats. gettotalrxbytes () = trafficstats. Unsupported ){
System. Out. println ("sorry, your mobile phone does not support traffic statistics ");
} Else {
Double effecetotal = trafficstats. gettotalrxbytes ()/1024.0/1024.0;
System. Out. println ("total traffic consumed by mobile phones:" + receivetotal + "M ");
}
// Total GPRS traffic consumed by mobile phones, in MB
If (trafficstats. getmobilerxbytes () = trafficstats. Unsupported ){
System. Out. println ("sorry, your mobile phone does not support traffic statistics ");
} Else {
Double receivegprs = trafficstats. getpolicerxbytes ()/1024.0/1024.0;
System. Out. println ("Total GPRS traffic consumed by mobile phones:" + receivegprs + "M ");
}
}
}