BroadcastReceiver模組
用於監聽開機資訊 並初始化和啟動服務
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class getpowerinfo extends BroadcastReceiver{
FileOutputStream out;
final public String ONPATH = "/data/data/zy.dnh/on.txt";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent bootActivityIntent=new Intent(context,mService1.class);//啟動服務
bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
writefile("0,0,0,0,0,0,0,0,0,0,0,0",ONPATH);
context.startService(bootActivityIntent);
Toast.makeText(context, "Netcounter service has been lauched", Toast.LENGTH_LONG).show();
Api.applySavedIptablesRules(context, false);//應用防火牆規則
Toast.makeText(context, "Wall rules have been lauched", Toast.LENGTH_LONG).show();
}
}
public void writefile(String str,String path )
{
File file;
try {
//創建檔
file = new File(path);
file.createNewFile();
//打開檔file的OutputStream
out = new FileOutputStream(file);
String infoToWrite = str;
//將字串轉換成byte陣列寫入檔
out.write(infoToWrite.getBytes());
//關閉檔file的OutputStream
out.close();
} catch (IOException e) {
//將出錯資訊列印到Logcat
}
mService1模組
後臺服務,用於維護流量日誌
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";//系統流量檔
String[] ethdata={"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};
String[] gprsdata={"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","0"};
String data="0,0,0,0,0,0,0,0,0,0,0,0";//對應on.txt裡面的格式
final String ETHLINE=" eth0";//乙太網資訊所在行
final String GPRSLINE="rmnet0";
final String WIFILINE="tiwlan0";
final String TEXT_ENCODING = "UTF-8";
final public String ONPATH = "/data/data/zy.dnh/on.txt";
final public String LOGPATH = "/data/data/zy.dnh/log.txt";
private Runnable mTasks = new Runnable()
{
public void run()//運行該服務執行此函數
{
refresh();
intCounter++;
// DisplayToast("Counter:"+Integer.toString(intCounter));
objHandler.postDelayed(mTasks, 30000);//每3000毫秒執行一次
}
};
@Override
public void onStart(Intent intent, int startId)
{
//writefile("0,0,0,0,0,0,0,0,0,0,0,0",ONPATH);//每次啟動服務 初始化onpath
objHandler.postDelayed(mTasks, 0);
super.onStart(intent, startId);
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onDestroy()
{
/* */
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("Could 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(GPRSLINE))
{
netdata=segs[1].trim().split(" ");
for(k=0,j=0;k<netdata.length;k++)
{
if(netdata[k].length()>0)
{
gprsdata[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{
//打開檔file的InputStream
file = new File(path);
in = new FileInputStream(file);
//將檔內容全部讀入到byte陣列
int length = (int)file.length();
byte[] temp = new byte[length];
in.read(temp, 0, length);
//將byte陣列用UTF-8編碼並存入display字串中
str = EncodingUtils.getString(temp,TEXT_ENCODING);
//關閉檔file的InputStream
in.close();
}
catch (IOException e) {
DisplayToast(e.toString());
}
return str;
}
public void writefile(String str,String path )
{
File file;
FileOutputStream out;
try {
//創建檔
file = new File(path);
file.createNewFile();
//打開檔file的OutputStream
out = new FileOutputStream(file);
String infoToWrite = str;
//將字串轉換成byte陣列寫入檔
out.write(infoToWrite.getBytes());
//關閉檔file的OutputStream
out.close();
} catch (IOException e) {
//將出錯資訊列印到Logcat
DisplayToast(e.toString());
}
}
public void refresh()
{
readdev();//讀取本次開機之後直到當前系統的總流量
data=ethdata[0]+","+ethdata[1]+","+ethdata[8]+","+ethdata[9]+","
+gprsdata[0]+","+gprsdata[1]+","+gprsdata[8]+","+gprsdata[9]+","
+wifidata[0]+","+wifidata[1]+","+wifidata[8]+","+wifidata[9];
String onstr=getinfo(ONPATH);//讀取on.txt記錄到onstr裡
String ondata[]=onstr.split(",");//將onstr各項分離 放到ondata裡
//計算增量
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(gprsdata[0])-Integer.parseInt(ondata[4]);
delta[5]=Integer.parseInt(gprsdata[1])-Integer.parseInt(ondata[5]);
delta[6]=Integer.parseInt(gprsdata[8])-Integer.parseInt(ondata[6]);
delta[7]=Integer.parseInt(gprsdata[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]);
//讀取log.txt
//獲取當前時間
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR); //獲取當前年份
mMonth = c.get(Calendar.MONTH)+1;//獲取當前月份
mDay = c.get(Calendar.DAY_OF_MONTH);//獲取當前月份的日期號碼
mHour = c.get(Calendar.HOUR_OF_DAY);//獲取當前的小時數
mMinute = c.get(Calendar.MINUTE);//獲取當前的分鐘數
mdate=mYear+"-"+mMonth+"-"+mDay;
String text=getinfo(LOGPATH);//將log.txt的內容讀到text字串中
String [] line=text.split("\n");
String today=line[line.length-1];//獲得今日已記錄流量
String [] beToday=today.split(",");
//檢查檔最後一行是否為今天的流量記錄資訊
if(!beToday[0].equals(mdate))//
//判斷今日流量是否已經記錄,如果今日流量沒有記錄
{
text=text+mdate+",0,0,0,0,0,0,0,0,0,0,0,0\n";
writefile(text,LOGPATH);
line=text.split("\n");
today=line[line.length-1];//獲得今日已記錄流量
beToday=today.split(",");
}
int i;
//處理今日流量
int [] newTodaydata=new int [12];//表示今日流量
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];//表示總流量數值
//更新第一行
String newtotal="total";
for(i=0;i<=11;i++)//更新今日流量和總流量
{
newTotaldata[i]=Integer.parseInt(beTotal[i+1])+delta[i];//總流量數值+delta[i]更新
newtotal=newtotal+","+newTotaldata[i];
}
newtotal= newtotal+"\n";
//處理中間不變的部分
String before="";//before為之前的從第1行到昨天的流量記錄
for(i=1;i<=line.length-2;i++)
before=before+line[i]+"\n";//代表中間不變的部分
String newlog=newtotal+before+newtoday;
writefile(data,ONPATH);//更新流量記錄
writefile(newlog,LOGPATH);//更新log*/
}
}
http://blog.csdn.net/Zengyangtech/archive/2010/06/01/5638604.aspx
全站熱搜
留言列表