[mobilesafe] 05_进程管理UI

Android 4.0

进程管理UI

技术点:
1、线性布局下, LinearLayout,如果上面的组件采用了
android:layout_width="match_parent"
android:layout_height="match_parent"
下面的组件也是一样,那么上面的会将下面的组件给覆盖掉,
所以要指定权重,值越大,权重越小,一般下面的要大点
android:layout_weight="1"

2、防止自己被选择,以及不让自己进程响应点击事件
解决:只要让对应item位置,进行判断,如果和当前手机卫士的包名一致,那么复选框不可选中,以及不可响应点击事件
a)屏蔽复选框
// 如果是自己,那么讲checkbox给隐藏掉
if (taskInfo.getAppPackageName().equals(getPackageName())) {// 如果item应用的包名和当前应用包名一致,为自己
    viewHolder.cb_task_status.setVisibility(View.INVISIBLE);// 不可见
}
b)屏蔽被选择
/**
 * 5、全选
 */
public void selectAll() {
    for (TaskInfo taskInfo : userTaskInfos) {
        if (taskInfo.getAppPackageName().equals(getPackageName())) {// 自己应用给跳过
            continue;
        }
        taskInfo.setChecked(true);
    }
    for (TaskInfo taskInfo : sysTaskInfos) {
        taskInfo.setChecked(true);
    }
    if (adataer != null) {
        adataer.notifyDataSetChanged();
    }
}
c)屏蔽点击事件
if (taskInfo.getAppPackageName().equals(getPackageName())) {// 自己应用,不可响应点击
    view.setClickable(false);
    return;
}
3、ListView中的checkbox的问题,上面的选中了,如果滚动到下面,下面的又被选中了,
这是由于复用了之前的convertView,所以下面复用的也被选中了。
解决:
if (taskInfo.isChecked()) {
    viewHolder.cb_task_status.setChecked(true);
else {
    viewHolder.cb_task_status.setChecked(false);
}
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 32、任务管理器Activity的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- 1、任务管理器标题 -->
    <TextView
        style="@style/wizard_textview_title"
        android:gravity="center"
        android:text="@string/task_manager" />
    <!-- 2、进程个数,可用/总内存 -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/tv_running_process_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/running_process_count" />
        <TextView
            android:id="@+id/tv_available_memory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="@string/avail_and_total_memory" />
    </RelativeLayout>
    <!-- TODO ListView显示等待加载数据的界面布局 -->
    <!-- 3、任务管理器ListView -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="300" >
        <ListView
            android:id="@+id/lv_task_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
        <LinearLayout
            android:id="@+id/ll_task_data_fill"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="invisible" >
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dip"
                android:gravity="center"
                android:text="@string/loadind_data_waiting" />
        </LinearLayout>
    </FrameLayout>
    <!-- 3、按钮 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
        <Button
            android:id="@+id/bt_task_selectAll"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_green_selector"
            android:text="@string/task_selectall"
            tools:ignore="NestedWeights" />
        <Button
            android:id="@+id/bt_task_clearAll"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_green_selector"
            android:text="@string/task_clearall" />
        <Button
            android:id="@+id/bt_task_selectOpposite"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_green_selector"
            android:text="@string/task_selectopposite" />
        <Button
            android:id="@+id/bt_task_clearMaster"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/btn_green_selector"
            android:text="@string/task_clearmaster" />
        <Button
            android:id="@+id/bt_task_setting"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_green_selector"
            android:text="@string/task_setting" />
    </LinearLayout>
</LinearLayout>
2、核心代码:
package cn.zengfansheng.mobilesafe;
 
import java.util.ArrayList;
import java.util.List;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.zengfansheng.mobilesafe.domain.TaskInfo;
import cn.zengfansheng.mobilesafe.engine.TaskInfoProvider;
import cn.zengfansheng.mobilesafe.utils.SystemInfoUtils;
 
/**
* 20、任务管理器Activity
* @author hacket
*/

public class TaskManageActivity extends Activity implements OnClickListener {
 
    private TextView tv_running_process_count;// 进程个数
    private TextView tv_available_memory;// 可用/总内存
 
    private ListView lv_task_list;// 任务管理器ListView
 
    private List<TaskInfo> taskInfos;// 当前所有进程的信息
 
    private View ll_task_data_fill;// 获取数据时等该的界面
 
    private List<TaskInfo> userTaskInfos;// 当前所有用户进程的信息
    private List<TaskInfo> sysTaskInfos;// 当前所有系统进程的信息
 
    // 按钮
    private Button bt_task_selectAll;// 全选
    private Button bt_task_clearAll;// 清空
    private Button bt_task_selectOpposite;// 反选
    private Button bt_task_clearMaster;// 一键清理
    private Button bt_task_setting;// 设置
 
    private MyTaskManageAdataer adataer;//任务管理器ListView数据适配器
 
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ll_task_data_fill.setVisibility(View.INVISIBLE);
 
            adataer = new MyTaskManageAdataer();
            lv_task_list.setAdapter(adataer);
        }
    };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_task_manager);
 
        tv_running_process_count = (TextView) this.findViewById(R.id.tv_running_process_count);
        tv_available_memory = (TextView) this.findViewById(R.id.tv_available_memory);
 
        lv_task_list = (ListView) this.findViewById(R.id.lv_task_list);
        ll_task_data_fill = this.findViewById(R.id.ll_task_data_fill);
 
        // 按钮初始化
        bt_task_selectAll = (Button) this.findViewById(R.id.bt_task_selectAll);
        bt_task_clearAll = (Button) this.findViewById(R.id.bt_task_clearAll);
        bt_task_selectOpposite = (Button) this.findViewById(R.id.bt_task_selectOpposite);
        bt_task_clearMaster = (Button) this.findViewById(R.id.bt_task_clearMaster);
        bt_task_setting = (Button) this.findViewById(R.id.bt_task_setting);
 
        // 1、当前运行的进程数
        int runningProcCount = SystemInfoUtils.getRunningProcCount(this);
        tv_running_process_count.setText(getResources().getString(R.string.running_process_count)+runningProcCount);
 
        // 2、可用/总内存
        String availableMemory = Formatter.formatFileSize(this,SystemInfoUtils.getAvailableMemory(this));
        String totalMemory = Formatter.formatFileSize(this,SystemInfoUtils.getTotalMemory(this));
        tv_available_memory.setText(getResources().getString(R.string.avail_and_total_memory)+availableMemory+"/"+totalMemory);
 
        // 3、获取数据,很耗时,所以要放子线程中进行
        fillData();
 
        // 4、注册item点击事件
        lv_task_list.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Object itemAtPosition = lv_task_list.getItemAtPosition(position);// 获取点击条目的对象,要和getItem()连用
                if (itemAtPosition !=null && itemAtPosition instanceof TaskInfo) {
                    TaskInfo taskInfo = (TaskInfo) itemAtPosition;
                    if (taskInfo.getAppPackageName().equals(getPackageName())) {// 自己应用,不可响应点击
                        view.setClickable(false);
                        return;
                    }
                    CheckBox cb_status = (CheckBox) view.findViewById(R.id.cb_task_status);
                    if (taskInfo.isChecked()) {//被选中
                        taskInfo.setChecked(false);
                        cb_status.setChecked(false);
                    }else {//未选中
                        taskInfo.setChecked(true);
                        cb_status.setChecked(true);
                    }
                }
            }
        });
 
        // 5、按钮点击事件
        bt_task_selectAll.setOnClickListener(this);
        bt_task_clearAll.setOnClickListener(this);
        bt_task_selectOpposite.setOnClickListener(this);
        bt_task_clearMaster.setOnClickListener(this);
        bt_task_setting.setOnClickListener(this);
    }
    /**
     * 9、设置
     */

    public void taskSetting(){
 
    }
    /**
     * 8、一键清理
     */

    public void clearMaster(){
 
    }
    /**
     * 7、反选
     */

    public void selectOpposite(){
        for (TaskInfo taskInfo : userTaskInfos) {
            if (taskInfo.getAppPackageName().equals(getPackageName())) {// 自己应用给跳过
                continue;
            }
            taskInfo.setChecked(!taskInfo.isChecked());
        }
        for (TaskInfo taskInfo : sysTaskInfos) {
            taskInfo.setChecked(!taskInfo.isChecked());
        }
        if (adataer != null) {
            adataer.notifyDataSetChanged();
        }
    }
    /**
     * 6、清空
     */

    public void clearAll(){
        for (TaskInfo taskInfo : userTaskInfos) {
            taskInfo.setChecked(false);
        }
        for (TaskInfo taskInfo : sysTaskInfos) {
            taskInfo.setChecked(false);
        }
        if (adataer != null) {
            adataer.notifyDataSetChanged();
        }
    }
    /**
     * 5、全选
     */

    public void selectAll() {
        for (TaskInfo taskInfo : userTaskInfos) {
            if (taskInfo.getAppPackageName().equals(getPackageName())) {// 自己应用给跳过
                continue;
            }
            taskInfo.setChecked(true);
        }
        for (TaskInfo taskInfo : sysTaskInfos) {
            taskInfo.setChecked(true);
        }
 
        if (adataer != null) {
            adataer.notifyDataSetChanged();
        }
    }
    // 4、按钮的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_task_selectAll:// 全选
            selectAll();
            break;
        case R.id.bt_task_clearAll:// 清空
            clearAll();
            break;
        case R.id.bt_task_selectOpposite:// 反选
            selectOpposite();
            break;
        case R.id.bt_task_clearMaster:// 一键清理
            clearMaster();
            break;
        case R.id.bt_task_setting:// 设置
            taskSetting();
            break;
        }
    }
    /**
     * 3、ListView的ViewHolder,用于保存查找到的组件的引用
     * @author hacket
     */

    private static class ViewHolder {
        private ImageView iv_task_icon;
        private TextView tv_task_app_name;
        private TextView tv_task_app_memsize;
        private CheckBox cb_task_status;
    }
    /**
     * 2、TaskManageActivity的ListView的适配器
     * @author hacket
     */

    private class MyTaskManageAdataer extends BaseAdapter {
 
        @Override
        public int getCount() {
            return userTaskInfos.size() + 1 + sysTaskInfos.size() + 1;
        }
        @Override
        public Object getItem(int position) {
            TaskInfo taskInfo;
            if (position == 0) {// 第0个位置,显示用户进程tv
                return null;
            } else if (position == userTaskInfos.size() + 1) {// 显示系统进程tv
                return null;
            } else if (position <= userTaskInfos.size()) { //用户进程
                taskInfo = userTaskInfos.get(position-1);
            } else { //系统进程
                taskInfo = sysTaskInfos.get(position - 1 - userTaskInfos.size()- 1);
            }
            return taskInfo;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
 
            TaskInfo taskInfo;
            if (position == 0) {// 第0个位置,显示用户进程tv
                TextView tView = new TextView(getApplicationContext());
                tView.setText("用户进程:" + userTaskInfos.size() + "个");
                tView.setTextSize(20);
                tView.setTextColor(Color.BLACK);
                return tView;
            } else if (position == userTaskInfos.size() + 1) {// 显示系统进程tv
                TextView tView = new TextView(getApplicationContext());
                tView.setTextSize(20);
                tView.setTextColor(Color.BLACK);
                tView.setText("系统进程:" + sysTaskInfos.size() + "个");
                return tView;
            } else if (position <= userTaskInfos.size()) { //用户进程
                taskInfo = userTaskInfos.get(position-1);
            } else { //系统进程
                taskInfo = sysTaskInfos.get(position-1-userTaskInfos.size()-1);
            }
            View view = null;
            ViewHolder viewHolder;
            if (convertView != null && convertView instanceof RelativeLayout) {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            } else {
 
                view = View.inflate(getApplicationContext(), R.layout.listview_taskinfo_item, null);
                viewHolder = new ViewHolder();
                viewHolder.iv_task_icon = (ImageView) view.findViewById(R.id.iv_task_icon);
                viewHolder.tv_task_app_name = (TextView) view.findViewById(R.id.tv_task_app_name);
                viewHolder.tv_task_app_memsize = (TextView) view.findViewById(R.id.tv_task_app_memsize);
                viewHolder.cb_task_status = (CheckBox) view.findViewById(R.id.cb_task_status);
                view.setTag(viewHolder);
            }
 
            viewHolder.iv_task_icon.setImageDrawable(taskInfo.getAppIcon());
            viewHolder.tv_task_app_name.setText(taskInfo.getAppName());
            long memSize = taskInfo.getMemSize();
            viewHolder.tv_task_app_memsize.setText("占用内存:"+Formatter.formatFileSize(getApplicationContext(), memSize));
            if (taskInfo.isChecked()) {
                viewHolder.cb_task_status.setChecked(true);
            } else {
                viewHolder.cb_task_status.setChecked(false);
            }
 
            // 如果是自己,那么讲checkbox给隐藏掉
            if (taskInfo.getAppPackageName().equals(getPackageName())) {// 如果item应用的包名和当前应用包名一致,为自己
                viewHolder.cb_task_status.setVisibility(View.INVISIBLE);// 不可见
            }
            return view;
        }
    }
 
    /**
     * 1、获取当前正在运行的进程的信息
     */

    public void fillData() {
        ll_task_data_fill.setVisibility(View.VISIBLE);
        new Thread() {
            public void run() {
                taskInfos = TaskInfoProvider.getTaskInfos(getApplicationContext());
                userTaskInfos = new ArrayList<TaskInfo>();
                sysTaskInfos = new ArrayList<TaskInfo>();
                for (TaskInfo taskInfo : taskInfos) {
                    if (taskInfo.isUserTask()) {// 用户进程
                        userTaskInfos.add(taskInfo);
                    } else {// 系统进程
                        sysTaskInfos.add(taskInfo);
                    }
                }
                handler.sendEmptyMessage(0);
            };
        }.start();
    }
}
效果: