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

How to do social private domain marketing well?

In 2021, more and more large companies will deplo...

International Energy Agency: Global Electric Vehicle Outlook 2019 (232 pages)

The International Energy Agency (IEA) publishes a...

It hasn't been long since Google's Project Ara was ruined

Editor's note: It's only been a short tim...

Why do some people get diarrhea when they are nervous?

Planning and production Source: A Brief History E...

Chen Jiangxiong-Douyin Follower Increase and Traffic Training Camp

Chen Jiangxiong's Douyin fan increase and tra...

Monument Valley design concept is learning is earning

We call those legendary, promising and unique com...

Controlling sugar does not mean quitting sugar, the real sugar control manual is here!

《Cotton Swab Medical Science Popularization》 Tao ...

The teacher was asked to resign and was sued for 420,000 yuan!

Article 23: An employer and an employee may agree...

Alibaba enters private banking market, BAT now lacks one

Yesterday afternoon, the China Banking Regulatory...

Why did Emperor Wen of Han want to be alone in Bailuyuan?

On the morning of December 14, the State Administ...