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 much does it cost to join the Zhangbei children's clothing mini program?

How much does it cost to join the Zhangbei childr...

What do WeChat Mini Programs mean to users and businesses?

In the process of establishing a new ecosystem, t...

Renewing domain name for a long time is helpful for ranking

In a patent application filed by Google in Decemb...

iPhone X will be criticized, but it will still be popular | On 4P and brand

The iPhone is criticized every year, so why does ...

How to operate and plan an offline event?

I participated in a two-day and one-night offline...

FB open-sources React Native, using JS to develop native iOS apps

Facebook today officially open-sourced the React ...

Tik Tok trending searches challenge Weibo's "advertising board"

Tik Tok’s trending searches are becoming more and...

30 excellent copywriting sentences all use this technique

The next 30 sentences all use the same technique....

YouTube App product analysis report!

This article is a product experience report of Yo...

Why are the seats in the subway designed to be slippery?

One minute with the doctor, the postures are cons...

7 techniques for shooting Douyin videos to promote products!

As a traffic pool with over 100 million daily act...

Weibo Brand Account Private Domain Marketing Skills

In the era of mobile Internet, traffic, that is, ...

Data operation: How to use data analysis to achieve user growth?

What does data analytics mean for growth? How to ...

Marketing activity planning and delivery

What I want to share with you today is the refine...