package com.example.wjfragmenttest;
import java.util.Iterator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends Activity {
private InitDataClass initData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
//使用内部类来构造初始化数据对象,使用对象来同步初始化数据防止onResume()和onCreate()中initData()的并发
//初始化数据封装对象
initData = new InitDataClass();
initData();//初始数据子线程
initEvent();
}
//用于处理back按键的返回数据
@Override
protected void onResume() {
initData();
super.onResume();
}
private void initData() {
//子线程获取数据
new Thread(){
public void run(){
//通过对象来初始化数据
initData.initData();
};
}.start();
}
private class InitDataClass{
//完成同步操作加synchronized
public synchronized void initData(){
//发送加载数据进度的消息
handler.obtainMessage(LOADING).sendToTargget();
//获取所有运行中的进程数据
List<TaskBean> allTaskDatas = TaskManagerEngine.getAllRunningTaskInfos(getApplicationContext());
availMem = TaskManagerEngine.getAvailMemSize(getApplicationContext());
totalMem = TaskManagerEngine.getTotalMemSize(getApplicationContext());
SystemClock.sleep(500);
sysTasks.clear();
userTasks.clear();
//分发数据
for (TaskBean taskBean:allTaskDatas) {
if(taskBean.isSystem()){
sysTasks.add(taskBean);//系统进程
}else{
userTask.add(taskBean);//用户进程
}
}
System.out.print(allTaskSize()+":"+sysTask.size()+userTask.size());
//加载数据完成
handler.obtainMessage(FINISH).sendToTarget();
}
}
}