Android Traffic Statistics Code

Source: Internet
Author: User
Package jdjsq. netcon; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class netcontralactivity extends activity {/** called when the activity is first created. */private button = NULL; fileoutputstream out; Final Public String onpath = "/data/ZY. dnh/on.txt "; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. button); button. listener (New buttonlistener ();} class buttonlistener implements onclicklistener {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent bootactivityintent = new intent (netcontralactivity. this, mservice1.class); // start the service bootactivityintent. addflags (intent. flag_activity_new_task); writefile ("0, 0, 0, 0, 0, 0, 0, 0, 0, 0", onpath); startservice (bootactivityintent); toast. maketext (netcontralactivity. this, "netcounter service has been lauched", toast. length_long ). show () ;}} public void writefile (string STR, string path) {file; try {// Create File file = new file (PATH); file. createnewfile (); // open the file's outputstream out = new fileoutputstream (File); string infotowrite = STR; // convert the string into a byte array and write it into the file out. write (infotowrite. getbytes (); // close the outputstream out of the file. close ();} catch (ioexception e) {// print the error message to logcat }}}
Package jdjsq. netcon; 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 {/* @ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL ;} */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/ne T/dev "; // system traffic file string [] ethdata = {" 0 "," 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, 0 "; // corresponds to the final string ethline =" eth0 "in the on.txt format; // The Ethernet information row final string uplsline =" rmnet0 "; Fina L 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, 30000); // execute once every 3000 milliseconds}; @ o Verride public void onstart (intent, int startid) {// todo auto-generated method stub // writefile ("0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ", onpath); // initialize onpath objhandler every time the service is started. 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) {maid [J] = netdata [k]; j ++ ;}} else if (line. startswith (W Ifiline) {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; string STR = ""; fileinputstream in; try {// enable inputstream 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 in. close ();} catch (ioexception e) {displaytoast (E. tostring ();} return STR;} public void writefile (string STR, string path) {file; fileoutputstream out; try {// Create File file = ne W file (PATH); file. createnewfile (); // open the file's outputstream out = new fileoutputstream (File); string infotowrite = STR; // convert the string into a byte array and write it into the file out. write (infotowrite. getbytes (); // close the outputstream out of the file. close ();} catch (ioexception e) {// print the error information to logcat displaytoast (E. tostring () ;}} public void refresh () {readdev (); // reads the total traffic data = ethdata [0] + until the current system is started ", "+ ethdata [1] +", "+ ethdata [8] +", "+ ethdata [9] + "," + maid [0] + "," + maid [1] + "," + maid [8] + ", "+ uplsdata [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 the onstr items into ondata. // calculate the incremental int [] Delta = new int [12]; Delta [0] = integer. parseint (ethdata [0])-integer. parseint (ondata [0]); Delta [1] = integer. parseint (ethdata [1])-integer. parseint (ondat A [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 (maid [0])-integer. parseint (ondata [4]); Delta [5] = integer. parseint (maid [1])-integer. parseint (ondata [5]); Delta [6] = integer. parseint (maid [8])-integer. parseint (ondata [6]); Delta [7] = integer. parseint (maid [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 string [] line = text in the text string. split ("/N"); string today = line [line. length-1]; // get today's recorded traffic string [] betoday = today. split (","); // check whether the last row of the file is today's traffic record information if (! Betoday [0]. equals (mdate) // determines whether the traffic today has been recorded. If the traffic today has not been recorded {text = text + mdate +, 0, 0/N "; writefile (text, logpath); line = text. split ("/N"); today = line [line. length-1]; // get today's recorded traffic betoday = today. split (",") ;}int I; // process today's traffic int [] newtodaydata = new int [12]; // represents today's traffic string newtoday = mdate; for (I = 0; I <= 11; I ++) // updates today's traffic {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]; // indicates the total traffic value // update the first line string newtotal = "Total "; for (I = 0; I <= 11; I ++) // updates today's traffic and total traffic {newtotaldata [I] = integer. parseint (betotal [I + 1]) + Delta [I]; // The total traffic value + Delta [I] updates newtotal = newtotal + "," + newtotaldata [I];} newtotal = newtotal + "/N"; // process the unchanged 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 string newlog = newtotal + before + newtoday; writefile (data, onpath); // update the traffic record writefile (newlog, logpath); // Update log */}}

Access system traffic records

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.