Read Android mobile phone traffic information

Source: Internet
Author: User

The Android mobile phone traffic information system is recorded in the/proc/self/NET/dev file in the Linux kernel.

 

Let's take a look at the dev file format.

 

Microsoft Windows XP [version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

 

D:/program files/Java/SDK/Android-SDK-Windows/tools> ADB Shell

# Cd proc

CD proc

# Cd net

CD net

# Cat Dev

Cat Dev

Inter-| receive | transmit

Face | bytes packets errs drop FIFO frame compressed multicast | bytes packe

TS errs drop FIFO colls carrier compressed

Lo: 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0

Eth0: 7069733 86239 0 0 0 0 12512463 741

79 0 0 0 0 0 0

Tunl0: 0 0 0 0 0 0 0 0

0 0 0 0 0 0

Gre0: 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0

#

 

 

We can read the traffic through this file.

The source code I read is as follows:

 

 

 

Package ZY. dnh;

 

Import java. Io. bufferedreader;

Import java. Io. file;

Import java. Io. fileinputstream;

Import java. Io. filenotfoundexception;

Import java. Io. fileoutputstream;

Import java. Io. filereader;

Import java. Io. ioexception;

Import java. util. calendar;

 

Import org. Apache. http. util. encodingutils;

 

Import Android. App. Service;

Import Android. content. intent;

Import Android. OS. Handler;

Import Android. OS. ibinder;

Import Android. widget. Toast;

 

/**/

Public class mservice1 extends Service

{

 

 

Private handler objhandler = new handler ();

Private int intcounter = 0;

Private int mhour;

Private int mminute;

Private int myear;

Private int mmonth;

Private int mday;

Private string mdate;

Final Public String dev_file = "/proc/self/NET/dev"; // system traffic File

String [] ethdata = {"0", "0", "0", "0", "0", "0", "0", "0 ", "0", "0", "0", "0", "0", "0", "0 "};

String [] maid = {"0", "0", "0", "0", "0", "0", "0", "0", "0 ", "0", "0", "0", "0", "0", "0", "0 "};

String [] wifidata = {"0", "0", "0", "0", "0", "0", "0", "0 ", "0", "0", "0", "0", "0", "0", "0 "};

String data = "0, 0, 0, 0, 0, 0, 0, 0, 0"; // corresponds to the format in on.txt

Final string ethline = "eth0"; // ETH indicates Ethernet information. tiwlan0 indicates WiFi, rmnet0 indicates GPRS.

Final string maid = "rmnet0 ";

Final string wifiline = "tiwlan0 ";

Final string text_encoding = "UTF-8 ";

 

Final Public String onpath = "/data/ZY. dnh/on.txt ";

Final Public String logpath = "/data/ZY. dnh/log.txt ";

Private runnable mtasks = new runnable ()

{

 

Public void run () // run the service to execute this function

{

Refresh ();

Intcounter ++;

// Displaytoast ("counter:" + integer. tostring (intcounter ));

Objhandler. postdelayed (mtasks, 3000); // executed every 3000 milliseconds

}

};

 

@ Override

Public void onstart (intent, int startid)

{

// Todo auto-generated method stub

 

Objhandler. postdelayed (mtasks, 0 );

Super. onstart (intent, startid );

}

 

@ Override

Public void oncreate ()

{

// Todo auto-generated method stub

 

Super. oncreate ();

}

 

@ Override

Public ibinder onbind (intent)

{

// Todo auto-generated method stub

 

 

Return NULL;

}

 

@ Override

Public void ondestroy ()

{

// Todo auto-generated method stub

 

/**/

Objhandler. removecallbacks (mtasks );

Super. ondestroy ();

}

Public void displaytoast (string Str)

{

Toast. maketext (this, STR, Toast. length_short). Show ();

}

Public void readdev ()

{

Filereader fstream = NULL;

Try {

Fstream = new filereader (dev_file );

 

}

Catch (filenotfoundexception e ){

Displaytoast ("cocould not read" + dev_file );

 

}

Bufferedreader in = new bufferedreader (fstream, 500 );

String line;

String [] segs;

String [] netdata;

 

Int COUNT = 0;

Int K;

Int J;

Try {

While (line = in. Readline ())! = NULL ){

Segs = line. Trim (). Split (":");

If (line. startswith (ethline ))

{

Netdata = segs [1]. Trim (). Split ("");

For (k = 0, j = 0; k <netdata. length; k ++)

{

If (netdata [K]. Length ()> 0)

{

 

Ethdata [J] = netdata [k];

J ++;

}

}

}

Else if (line. startswith (uplsline ))

{

Netdata = segs [1]. Trim (). Split ("");

For (k = 0, j = 0; k <netdata. length; k ++)

{


If (netdata [K]. Length ()> 0)

{

 

GPRS data [J] = netdata [k];

J ++;

}

}

}

Else if (line. startswith (wifiline ))

{

Netdata = segs [1]. Trim (). Split ("");

For (k = 0, j = 0; k <netdata. length; k ++)

{

If (netdata [K]. Length ()> 0)

{

 

Wifidata [J] = netdata [k];

J ++;

}

}

}

 

 

 

 

Count ++;

}

Fstream. Close ();

 

}

Catch (ioexception e ){

Displaytoast (E. tostring ());

}

}

Public String getinfo (string path)

{

File file;

String STR = "";

Fileinputstream in;

Try {

// Open the inputstream of the file

File = new file (PATH );

In = new fileinputstream (File );

// Read all the file content into the byte array

Int length = (INT) file. Length ();

Byte [] temp = new byte [length];

In. Read (temp, 0, length );

// Encode the byte array with a UTF-8 and store it in the display string

STR = encodingutils. getstring (temp, text_encoding );

// Close the inputstream of the file

 

In. Close ();

}

Catch (ioexception e ){

 

Displaytoast (E. tostring ());

 

}

Return STR;

}

Public void writefile (string STR, string path)

{

File file;

Fileoutputstream out;

Try {

// Create a file

File = new file (PATH );

File. createnewfile ();

// Open the outputstream of the file

Out = new fileoutputstream (File );

String infotowrite = STR;

// Convert the string into a byte array and write it to the file

Out. Write (infotowrite. getbytes ());

// Close the outputstream of the file

Out. Close ();

} Catch (ioexception e ){

// Print error information to logcat

Displaytoast (E. tostring ());

 

}

 

}

 

Public void refresh ()

{

Readdev (); // read the total traffic from the current system after the current Boot

Data = ethdata [0] + "," + ethdata [1] + "," + ethdata [8] + "," + ethdata [9] + ","

+ Maid [0] + "," + maid [1] + "," + maid [8] + "," + maid [9] + ","

+ Wifidata [0] + "," + wifidata [1] + "," + wifidata [8] + "," + wifidata [9];

String onstr = getinfo (onpath); // read the on.txt record to onstr

String ondata [] = onstr. Split (","); // separate onstr items and put them in ondata

// Computing Increment

Int [] Delta = new int [12];

Delta [0] = integer. parseint (ethdata [0])-integer. parseint (ondata [0]);

Delta [1] = integer. parseint (ethdata [1])-integer. parseint (ondata [1]);

Delta [2] = integer. parseint (ethdata [8])-integer. parseint (ondata [2]);

Delta [3] = integer. parseint (ethdata [9])-integer. parseint (ondata [3]);

Delta [4] = integer. parseint (uplsdata [0])-integer. parseint (ondata [4]);

Delta [5] = integer. parseint (uplsdata [1])-integer. parseint (ondata [5]);

Delta [6] = integer. parseint (uplsdata [8])-integer. parseint (ondata [6]);

Delta [7] = integer. parseint (uplsdata [9])-integer. parseint (ondata [7]);

Delta [8] = integer. parseint (wifidata [0])-integer. parseint (ondata [8]);

Delta [9] = integer. parseint (wifidata [1])-integer. parseint (ondata [9]);

Delta [10] = integer. parseint (wifidata [8])-integer. parseint (ondata [10]);

Delta [11] = integer. parseint (wifidata [9])-integer. parseint (ondata [11]);

// Read log.txt

// Obtain the current time

Final calendar c = calendar. getinstance ();

Myear = C. Get (calendar. year); // obtain the current year

Mmonth = C. Get (calendar. month) + 1; // obtain the current month

Mday = C. Get (calendar. day_of_month); // obtain the Date Number of the current month

Mhour = C. Get (calendar. hour_of_day); // obtain the current hour

Mminute = C. Get (calendar. Minute); // obtain the current number of minutes

Mdate = myear + "-" + mmonth + "-" + mday;

 

String text = getinfo (logpath); // read the content of log.txt to the text string

String [] line = text. Split ("/N ");

 

String today = line [LINE. Length-1]; // obtain the recorded traffic today

String [] betoday = today. Split (",");

// Check whether the last row of the file is the traffic record of today

If (! Betoday [0]. Equals (mdate) // if the file contains only one row, the current log is empty and the current date is added

// Determine whether today's traffic has been recorded. If today's traffic is not recorded

{

 

TEXT = text + mdate + ", 0, 0, 0, 0, 0, 0, 0, 0, 0/N ";

Writefile (text, logpath );

Line = text. Split ("/N ");

Today = line [LINE. Length-1]; // obtain the recorded traffic today

 

Betoday = today. Split (",");

}

 

Int I;

// Process today's traffic

Int [] newtodaydata = new int [12]; // Today's traffic

String newtoday = mdate;

For (I = 0; I <= 11; I ++)

{

Newtodaydata [I] = integer. parseint (betoday [I + 1]) + Delta [I];

Newtoday = newtoday + "," + newtodaydata [I];

}

Newtoday = newtoday + "/N ";

String [] betotal = line [0]. Split (",");

Int [] newtotaldata = new int [12]; // The total traffic value.

// Update the first line

String newtotal = "Total ";

For (I = 0; I <= 11; I ++)

{

Newtotaldata [I] = integer. parseint (betotal [I + 1]) + Delta [I]; // The total traffic value + Delta [I] is updated.

Newtotal = newtotal + "," + newtotaldata [I];

}

Newtotal = newtotal + "/N ";

// Process the unchanged intermediate part

String before = ""; // before is the previous traffic record from 1st rows to yesterday

For (I = 1; I <= line. Length-2; I ++)

Before = before + line [I] + "/N"; // represents the unchanged part in the middle.

String newlog = newtotal + before + newtoday;

 

Writefile (data, onpath); // update traffic records

Writefile (newlog, logpath); // Update log */

}

 

 

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Zengyangtech/archive/2010/05/04/5554940.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.