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 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 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 */
 
 
 
}
 
 
}

Author "technical life"

Related Article

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.