首先创建并注册TaskManager主页面,其次绑定task_manager的layout,由于是ListView所以需要一个task_item页面来进行列表元素设置,一下是两个页面代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/task_manager">
<TextView
style="@style/text_title_style"
android:text="任 务 管 理" />
<View style="@style/view_divide_line_style" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<TextView
android:id="@+id/tv_task_manager_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="进程数:20"/>
<TextView
android:id="@+id/tv_task_manager_task_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="15dp"
android:text="可用/总内存:50MB/700MB"/>
RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_task_manager_noSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:layout_alignParentRight="true"
android:textSize="20dp"
/>
<Button
android:id="@+id/bt_task_manager_selectAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_toLeftOf="@id/bt_task_manager_noSelect"
android:textSize="20dp"
/>
RelativeLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/lv_taskmanager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#fff"
android:fastScrollEnabled="true"
android:dividerHeight="1.0dip"
android:paddingLeft="3.0dip"
android:paddingRight="3.0dip" />
<RelativeLayout
android:id="@+id/rl_loading"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:id="@+id/linearLayout">
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="正在加载应用程序。。"
android:textColor="#fff"
android:textSize="22sp" />
LinearLayout>
RelativeLayout>
FrameLayout>
LinearLayout>
因为是ListView所以绑定适配器TaskManagerAdapter
class TaskManagerAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public TaskManagerAdapter() {
// TODO Auto-generated constructor stub
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return taskInfos.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return taskInfos.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
ViewHolder holder = null;
if(convertView != null){
view = convertView;
holder = (ViewHolder) view.getTag();
}else{
view = mInflater.inflate(R.layout.task_item, null);
holder = new ViewHolder();
holder.iv_task_manager_icon = (ImageView) view.findViewById(R.id.iv_task_manager_icon);
holder.tv_task_manager_name = (TextView) view.findViewById(R.id.tv_task_manager_name);
holder.tv_task_manager_memory = (TextView) view.findViewById(R.id.tv_task_manager_memory);
}
TaskInfo taskInfo = taskInfos.get(position);
holder.iv_task_manager_icon.setImageDrawable(taskInfo.getTask_icon());
holder.tv_task_manager_name.setText(taskInfo.getTask_name());
holder.tv_task_manager_memory.setText("占用的内存:" + TextFormat.formatByte(taskInfo.getTask_memory()*1024));
return view;
}
}
static class ViewHolder{
ImageView iv_task_manager_icon;
TextView tv_task_manager_name;
TextView tv_task_manager_memory;
}
其次就是获取应用程序的信息了,所以需要一个TaskInfo类
public class TaskInfo {
private Drawable task_icon;
private String task_name;
private String packageName;
private int pid;
private long task_memory;
public TaskInfo() {
super();
// TODO Auto-generated constructor stub
}
public TaskInfo(Drawable task_icon, String task_name, String packageName,
int pid, long task_memory) {
super();
this.task_icon = task_icon;
this.task_name = task_name;
this.packageName = packageName;
this.pid = pid;
this.task_memory = task_memory;
}
public Drawable getTask_icon() {
return task_icon;
}
public void setTask_icon(Drawable task_icon) {
this.task_icon = task_icon;
}
public String getTask_name() {
return task_name;
}
public void setTask_name(String task_name) {
this.task_name = task_name;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public long getTask_memory() {
return task_memory;
}
public void setTask_memory(long task_memory) {
this.task_memory = task_memory;
}
@Override
public String toString() {
return "TaskInfo [task_icon=" + task_icon + ", task_name=" + task_name
+ ", packageName=" + packageName + ", pid=" + pid
+ ", task_memory=" + task_memory + "]";
}
}
接下来就是逻辑功能类TaskUtil的编写了,分别是获取运行的程序的数量,获取运行的程序信息,获取系统可用内存,获取系统总内存。
public class TaskUtil {
/**
* 得到当前正在运行的进程的数量
* @param context
* @return*/
public static int getRuninngAppProcessInfoSize(Context context){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return ProcessManager.getRunningAppProcesses().size();
// return ActivityManager.
}
/**
* 得到当前系统的总内存
* @param
* @return*/
public static long getTotalMemSize() {
long size=0;
File file = new File("/proc/meminfo");
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String memInfo = buffer.readLine();
int startIndex = memInfo.indexOf(":");
int endIndex = memInfo.indexOf("k");
memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
size = Long.parseLong(memInfo);
size *= 1024;
buffer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return size;
}
/**
* 得到当前系统的可用内存
* @param
* @return*/
public static long getAvailMem(Context context){
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
long availMem = outInfo.availMem/1; // byte
return availMem;
}
public static long getAviableMemSize() {
long size=0;
File file = new File("/proc/meminfo");
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String memInfos=new String();
String memInfo = null;
int i=0;
while ((memInfos=buffer.readLine())!=null){
i++;
if (i==2){
memInfo = memInfos;
}
}
int startIndex = memInfo.indexOf(":");
int endIndex = memInfo.indexOf("k");
memInfo = memInfo.substring(startIndex + 1, endIndex).trim();
size = Long.parseLong(memInfo);
buffer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return size;
}
//得到进程所有的信息
public static List getTaskInfos(Context context){
List taskInfos = new ArrayList();
//应用程序包管理器
PackageManager pm = context.getPackageManager();
//应用程序管理器
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List runningAppProcessInfos = ProcessManager.getRunningAppProcesses();
for (AndroidAppProcess info:runningAppProcessInfos){
TaskInfo taskInfo = new TaskInfo();
String packageName = info.name;
taskInfo.setPackageName(packageName);
try {
ApplicationInfo appInfo = pm.getApplicationInfo(packageName,0);
Drawable task_icon = appInfo.loadIcon(pm);
String task_name = appInfo.loadLabel(pm).toString();
if(task_icon == null){
taskInfo.setTask_icon(context.getResources().getDrawable(R.drawable.tubiao));
}else {
taskInfo.setTask_icon(task_icon);
}
taskInfo.setTask_name(task_name);
}catch (Exception e){
e.printStackTrace();
// 系统内核进程 没有名称
taskInfo.setTask_name(packageName);
Drawable icon = context.getResources().getDrawable(
R.mipmap.ic_launcher);
taskInfo.setTask_icon(icon);
}
//获取程序内存
int pid = info.pid;
taskInfo.setPid(pid);
Debug.MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(new int[]{info.pid});
Debug.MemoryInfo memoryInfo = memoryInfos[0];
long task_memory = memoryInfo.getTotalPrivateDirty();//KB单位
taskInfo.setTask_memory(task_memory);
if (taskInfo != null) {
taskInfos.add(taskInfo);
}
}
return taskInfos;
}
}
需要注意的是安卓5.0版本后就无法通过ActivityManager来获取运行程序信息了,需要用ProcessManager类来获取运行程序信息,所以需要通过该网页下载下面包导入才行。
http://download.csdn.net/detail/luoyangwyb/9241279
该网址是来自于一个高手的博客分享。
还有一个根据数值大小转换成不同的单位的类
public class TextFormat {
public static String formatByte(long size) {
//如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
if (size < 1024) {
return String.valueOf(size) + "byte";
} else {
size = size / 1024;
}
//如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
//因为还没有到达要使用另一个单位的时候
//接下去以此类推
if (size < 1024) {
return String.valueOf(size) + "KB";
} else {
size = size / 1024;
}
if (size < 1024) {
//因为如果以MB为单位的话,要保留最后1位小数,
//因此,把此数乘以100之后再取余
size = size * 100;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "MB";
} else {
//否则如果要以GB为单位的,先除于1024再作同样的处理
size = size * 100 / 1024;
return String.valueOf((size / 100)) + "."
+ String.valueOf((size % 100)) + "GB";
}
}
}
接下来就是最终的主页面TaskManager类的编写了。
通过onCreate方法加载页面,然后找就绑定id为监听做准备。接下来就是调用程序运行数量,再开新线程去传输加载完成新号,来通过Handler接受数据并处理页面,当通过加载完成时,调用可用内存和总内存方法,隐藏加载进度条,并显示ListView即可
public class TaskManager extends Activity {
protected static final int SUCCESS_GET_TASKINFO = 0;
private TextView tv_task_manager_task_count;
private TextView tv_task_manager_task_memory;
private RelativeLayout rl_loading;
private ListView lv_taskmanager;
private boolean isChecked;
private ActivityManager am;
private List taskInfos;
private SharedPreferences sp;
private TaskManagerAdapter mAdapter;
private LinearLayout task_manager;
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS_GET_TASKINFO:
//可用内存
String availMemStr = TextFormat.formatByte(TaskUtil.getAvailMem(getApplication()));
//总内存
String totalMemStr = TextFormat.formatByte(TaskUtil.getTotalMemSize());
tv_task_manager_task_memory.setText("可用/总内存:" + availMemStr +"/" + totalMemStr);
mAdapter = new TaskManagerAdapter();
rl_loading.setVisibility(View.GONE);
lv_taskmanager.setAdapter(mAdapter);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_manager);
tv_task_manager_task_count = (TextView) findViewById(R.id.tv_task_manager_task_count);
tv_task_manager_task_memory = (TextView) findViewById(R.id.tv_task_manager_task_memory);
rl_loading = (RelativeLayout) findViewById(R.id.rl_loading);
lv_taskmanager = (ListView) findViewById(R.id.lv_taskmanager);
task_manager = (LinearLayout) findViewById(R.id.task_manager);
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// 进程数目
int size = TaskUtil.getRuninngAppProcessInfoSize(this);
tv_task_manager_task_count.setText("进程数:" + size);
new Thread(){
@Override
public void run() {
taskInfos = TaskUtil.getTaskInfos(getApplicationContext());
Message msg = new Message();
msg.what = SUCCESS_GET_TASKINFO;
mHandler.sendMessage(msg);
}
}.start();
}
}
今天还没有做完,仅仅完成显示运行程序的功能,并没有实现程序结束选定分类等功能。 接下来继续努力吧。