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

Product operation strategy: How to carry out user stratification operations?

Pyramid stratification is a method of stratifying...

What does Internet TV rely on to “fight” with the Olympic season?

"Humanity has five universal languages: mone...

How should the ad landing page be designed to improve conversions?

The author of this article will start from the bi...

How to quickly build a user growth system from 0 to 1?

The user growth system has always been a necessar...

Recommended by experts: iOS custom drop-down line animation

This is the second demo in this chapter. In this ...