Looper.prepare(); final Handler handler = new Handler(); Runnable runnable = new Runnable(){ private String M; SimpleDateFormat formatter =new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date curDate; String date;
@Override public void run() { // TODO Auto-generated method stub //获取当前时间 curDate = new Date(System.currentTimeMillis()); date = formatter.format(curDate) + "\n"; outInfo("\n" + date);
在哪里开启的线程?
public void monitor(final Context context){
getRunningProcessInfo(context);
Looper.prepare();
final Handler handler = new Handler();
Runnable runnable = new Runnable(){
private String M;
SimpleDateFormat formatter =new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date curDate;
String date;
@Override
public void run() {
// TODO Auto-generated method stub
//获取当前时间
curDate = new Date(System.currentTimeMillis());
date = formatter.format(curDate) + "\n";
outInfo("\n" + date);
for(int i=1; UID[i]>0; i++){
newFlow[i] = TrafficStats.getUidRxBytes(UID[i])/1024;
usedFlow[i] = 0;
if((newFlow[i]>oldFlow[i])){
usedFlow[i] = newFlow[i] - oldFlow[i];
oldFlow[i] = newFlow[i];
M = String.valueOf(usedFlow[i])+"KB ";
outInfo(M);
outInfo(log[i]);
}
}
getRunningProcessInfo(context);
for(int j=1; j<packageName.length-1; j++){
if(packageName[j]!=""){
if(isTopActivity(context, packageName[j])){
outInfo("foregroundTask=" + packageName[j] + "\n");
}
}
}
handler.postDelayed(this, time*1000);// 10000是延时时长
}
};
handler.postDelayed(runnable, time*1000);// 打开定时器,执行操作
//handler.removeCallbacks(this);// 关闭定时器处理
}
这里就是monitor里的代码,求解啊
这两个是同样的代码 一个是从activity里实例化的函数 一个就是在service里的函数。
新的线程是在service里开启的
我是新手,不是很懂,我想的是service后台运行,用一个新的线程,这样清晰一些