Traffic monitoring of Android application source code

Traffic monitoring of Android application source code

Source code introduction <br /> A small example found on the Internet, which can count the network traffic of each application and distinguish between mobile network and WIFI. Technical points: Use TrafficStats to record traffic changes. Use Service, BroadcastReceiver to monitor network status changes. Use sqlite to record the traffic data used by each application (only the traffic used after the application is installed can be counted, and the traffic used before cannot be counted).
Source code running screenshot

Code snippet:

  1. @Override  
  2. protected   void onCreate(Bundle savedInstanceState) {
  3. super .onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5.   
  6. txtView = (TextView) findViewById(R.id.textView1);
  7.   
  8. Intent intent = new Intent(MainActivity. this , TrafficService. class );
  9. bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  10.   
  11. dbManager = new DbManager( this );
  12. findViewById(R.id.button1).setOnClickListener( new OnClickListener() {
  13.   
  14. @Override  
  15. public   void onClick(View v) {
  16. if (trafficService == null ) {
  17. txtView.setText( "Service not bound" );
  18. } else {
  19. trafficService.logRecord();
  20. Map<string, trafficinfo= "" > list = dbManager.queryTotal();
  21. StringBuilder sb = new StringBuilder();
  22. for (TrafficInfo info : list.values()) {
  23. sb.append(info.appName + " - traffic information:\r\n" );
  24. sb.append(
  25. "Mobile network traffic received"  
  26. + Formatter.formatFileSize(
  27. MainActivity.this ,
  28. info.mobileRx)).append( "\r\n" );
  29. sb.append(
  30. "Traffic sent by mobile network"  
  31. + Formatter.formatFileSize(
  32. MainActivity.this ,
  33. info.mobileTx)).append( "\r\n" );
  34. sb.append(
  35. "Wi-Fi received traffic"  
  36. + Formatter.formatFileSize(
  37. MainActivity. this , info.wifiRx))
  38. .append( "\r\n" );
  39. sb.append(
  40. "Wi-Fi traffic"  
  41. + Formatter.formatFileSize(
  42. MainActivity. this , info.wifiTx))
  43. .append( "\r\n" );
  44. sb.append( "--------------------" ).append( "\r\n" );
  45. txtView.setText(sb);
  46. }
  47. }
  48. }
  49. });
  50. }</string,>

Source code link: http://download..com/900943

<<:  SwipeMenuListView list sliding function

>>:  An image picker that mimics iMessage in iOS8

Recommend

Flower Pig’s fission activity method!

In the first half of the year, Hua Xiaozhu came o...

Build a secure app! iOS security series: HTTPS advanced

[[149556]] This article is divided into the follo...

Indians are chasing Chinese mobile phones, thinking Xiaomi is an Indian brand

According to a report on May 2, foreign media rep...

The matching method of Baidu SEM promotion keywords has been upgraded!

What is keyword matching? Just like Cupid’s arrow ...

WeChat has updated a dozen new features, many of which are really useful

Hello friends, today I woke up and found that iOS...

Marketing promotion: How far are "HEYTEA" from Starbucks?

At the beginning of the year, Ma Dong accepted an...

Toothache, sore throat, neck and back pain may be your heart sounding the alarm!

Staying up late, working overtime, and being unde...

Look here, how a marine life version of "Game of Thrones" is being staged?

The coral reef area of ​​the Indian Ocean and the...

The science behind hangovers is actually...

According to a report on the U.S. News Weekly web...

The Douyin operation plan is here!

Where are the opportunities for Douyin? Basically...