标签:
本节主要介绍进程管理界面的逻辑代码:至此进程管理的功能已经完善了
首先要加载正在运行的进程,获取进程信息, 这是一个耗时的操作,
所以一个子线程来处理缓冲加载, 加载完数据就通知ListView 将数据展示到
界面,listview怎么知道,数据缓冲完了,如何获取这个通知呢?
不错就是使用Thread和Handler结合,在缓冲完成后,使用handler发送一个
空消息 handler.sendEmptyMessage(1);
/** * 缓冲加载进程数据 */ private void initData() { //显示缓冲加载进程 linearLayout.setVisibility(View.VISIBLE); //缓冲加载是一个耗时的操作,使用一个子线程操作,通过handler通知。 new Thread(new Runnable() { @Override public void run() { //获取所有正在运行的进程 mProcess = ProcessInfoProvider.getRunningProcess(ProcessManagerActivity.this); //加载完成后,通知ListView展示数据 handler.sendEmptyMessage(1); } }).start(); runningProgress =ProcessInfoProvider.getRunningProcessNumber(this); availMemory = ProcessInfoProvider.getAvailMenory(ProcessManagerActivity.this); totalMemory = ProcessInfoProvider.getAllAvailMenory(ProcessManagerActivity.this); tvRunningProgress.setText("运行中的进程:"+runningProgress+"个"); tv_avail.setText("剩余/总内存:" + Formatter.formatFileSize(this, availMemory) + "/" + Formatter.formatFileSize(this, totalMemory)); }
Handler等待处理发送过来的消息, 当获取msg.what == 1
说明缓冲加载完成, 可以进行listView的初始化,及适配器加载,
private Handler handler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1){//加载listView lv = (ListView) findViewById(R.id.amp_pl1_lv1); adapter = new ProgressInfoAdapter(ProcessManagerActivity.this, mProcess); lv.setAdapter(adapter); adapter.notifyDataSetChanged(); userPrcessNum = adapter.getUserProcess().size(); tv_header.setText("用户进程("+userPrcessNum+")"); linearLayout.setVisibility(View.GONE);//加载数据完成后,缓冲消失 //初始化listView的事件监听 initListViewEvent(); } }; };
package com.chb.myphonesave.activity; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.format.Formatter; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.chb.myphonesave.R; import com.chb.myphonesave.adapter.ProgressInfoAdapter; import com.chb.myphonesave.entity.ProcessInfo; import com.chb.myphonesave.provider.ProcessInfoProvider; /** * 进程管理 Actvity * 首先显示缓冲加载界面,获取进程信息, * * 获取完成后, 通知ListView显示 */ public class ProcessManagerActivity extends Activity { private TextView tvRunningProgress; //正在运行的进程数 private TextView tv_avail;//剩余进程,和总内存大小 private TextView tv_header;//ListView头部显示用户和系统进程 private int runningProgress; //正在运行进程的个数 private long availMemory; //可用内存 private long totalMemory; //总内存 private List<ProcessInfo> mProcess; private ListView lv; private ProgressInfoAdapter adapter; private LinearLayout linearLayout;//缓冲加载的线性布局 private int userPrcessNum;//用户进程数目 private Handler handler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1){//加载listView lv = (ListView) findViewById(R.id.amp_pl1_lv1); adapter = new ProgressInfoAdapter(ProcessManagerActivity.this, mProcess); lv.setAdapter(adapter); adapter.notifyDataSetChanged(); userPrcessNum = adapter.getUserProcess().size(); tv_header.setText("用户进程("+userPrcessNum+")"); linearLayout.setVisibility(View.GONE);//加载数据完成后,缓冲消失 //初始化listView的事件监听 initListViewEvent(); } }; }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_progressmanager); //初始化view initView(); //加载进程数据 initData(); } /** * 初始化View */ private void initView() { tvRunningProgress = (TextView) findViewById(R.id.apm_rl1_tv1); tv_avail = (TextView) findViewById(R.id.apm_rl1_tv2); tv_header = (TextView) findViewById(R.id.tv_header); linearLayout = (LinearLayout) findViewById(R.id.apm_pl1_ll1); } /** * 初始化listView的事件监听 */ private void initListViewEvent() { /** * 子项点击事件监听 */ lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ProcessInfo pf= adapter.getItem(position); pf.isChecked = !pf.isChecked; adapter.notifyDataSetChanged(); for(ProcessInfo p: mProcess){ Log.i("tag", p+""); } } }); /** * ListView 滚动事件监听 */ lv.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //Log.i("tag", "--->"+firstVisibleItem +"----->"+visibleItemCount); if(mProcess != null){ if(firstVisibleItem >= userPrcessNum + 1){ tv_header.setText("系统进程("+(mProcess.size() - userPrcessNum)+")"); }else { tv_header.setText("用户进程("+userPrcessNum+")"); } } } }); } /** * 缓冲加载进程数据 */ private void initData() { //显示缓冲加载进程 linearLayout.setVisibility(View.VISIBLE); //缓冲加载是一个耗时的操作,使用一个子线程操作,通过handler通知。 new Thread(new Runnable() { @Override public void run() { //获取所有正在运行的进程 mProcess = ProcessInfoProvider.getRunningProcess(ProcessManagerActivity.this); //加载完成后,通知ListView展示数据 handler.sendEmptyMessage(1); } }).start(); runningProgress =ProcessInfoProvider.getRunningProcessNumber(this); availMemory = ProcessInfoProvider.getAvailMenory(ProcessManagerActivity.this); totalMemory = ProcessInfoProvider.getAllAvailMenory(ProcessManagerActivity.this); tvRunningProgress.setText("运行中的进程:"+runningProgress+"个"); tv_avail.setText("剩余/总内存:" + Formatter.formatFileSize(this, availMemory) + "/" + Formatter.formatFileSize(this, totalMemory)); } /** * 全选事件监听 * @param v */ public void selectAll(View v){ if(mProcess == null){ return; } for (ProcessInfo p : mProcess) { if(getPackageName().equals(p.getPackageName())){ //不要删了自己 }else { p.isChecked = true; } //通知适配器 adapter.notifyDataSetChanged(); } } /** * 反选事件监听 * @param v */ public void unSelect(View v) { if(mProcess == null){ return; } for (ProcessInfo p : mProcess) { if(getPackageName().equals(p.getPackageName())){ //不要删了自己 }else { p.isChecked = ! p.isChecked; } //通知适配器 adapter.notifyDataSetChanged(); } } /** * 一键清理事件监听 * 需要权限:android.permission.KILL_BACKGROUND_PROCESSES * @param v */ public void cleanAllSelect(View v){ ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); //保存已经杀死进程的集合 List<ProcessInfo> killList=new ArrayList<ProcessInfo>(); long memory=0;//杀死进程的大小 int count = 0; if(mProcess == null){ return; } for (ProcessInfo p : mProcess) { if(getPackageName().equals(p.getPackageName())){ //不要删了自己 }else { if(p.isChecked == true){ Log.i("tag", p+""); killList.add(p); manager.killBackgroundProcesses(p.getPackageName());//杀死进程 /*不要在遍历的同时,删除数组元素 * 会出现Caused by: java.util.ConcurrentModificationException * mProcess.remove(p); * */ memory += p.getMomery(); count++; } } } for(ProcessInfo Pe : killList){ mProcess.remove(Pe); } Toast.makeText(getApplicationContext(), "杀死进程:"+count+"个,共节省内存:"+ Formatter.formatFileSize(getApplicationContext(), memory)+"空间" , Toast.LENGTH_SHORT).show(); //更新进程个数和内存数 tvRunningProgress.setText("运行中的进程:"+(runningProgress-count)); tv_avail.setText("剩余/总内存:"+Formatter.formatFileSize(this, availMemory+memory)+"/"+ Formatter.formatFileSize(this, totalMemory)); //通知适配器 adapter.notifyDataSetChanged(); } /** * 跳转到设置的事件监听 * @param v */ public void onSettings(View v) { } }
标签:
原文地址:http://www.cnblogs.com/chengbao/p/5641290.html