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 does belly fat form? How can you lose it?

This article was reviewed by Tao Ning, PhD, Assoc...

I will never forget this day!

July 7, 2022 It is the 85th anniversary of the Ju...

Apple's ugliest designs this year: The new MacBook tops the list

There is no doubt that Apple's industrial desi...

Stickman's big screen counterattack "Stickman Tennis 2015" TV version review

Screen: Sound Effects: operate: Plot: Experience:...

Monkeypox spreads to 30 countries. Will it become a new pandemic?

It is indeed a challenge, but there is no need to...

Why is it that the articles you write are very valuable but no one reposts them?

Take “Cool Play Lab”, an article with over 100,000...