Android-killing the process selected by the user for Optimization

Source: Internet
Author: User

Android-killing the process selected by the user for Optimization

 

In the previous blog post "Android: killing the process selected by the user (releasing the space occupied by the process)", we introduced how to kill the process selected by the user. However, there is a problem left over, that is, after the process is killed, the ListVIew list is not refreshed immediately. In this article, we will solve this problem and optimize the display of ListView to improve the user experience. Our blog post is also optimized based on the previous blog. Please read the previous blog post "Android-killing the process selected by the user (releasing the space occupied by the process)".

I. Principles

Here, we mainly optimize the custom adapter class in the TaskManagerActivity class. In this custom adapter class, we add an attribute Field List Infos, this set indicates the data set to be displayed in ListView, and sets a set method for this set. When the data set is changed elsewhere, for example, after adding or removing data from a set, you only need to call the set Method of the custom adapter to transfer the updated list set to the custom adapter class. At the same time, you can call the notifyDataSetChanged () method to refresh the ListView page.

II. Implementation 1. Update the custom adapter TaskManagerAdapter

In this custom adapter class, we added an attribute Field List Infos, this set indicates the data set to be displayed in ListView, and sets a set method for this set. When the data set is changed elsewhere, for example, after adding or removing data from the set, you only need to call the set Method of the custom adapter to pass the updated list set to the custom adapter class.

The specific implementation code is as follows:

 

/*** Custom adapter ** @ author liuyazhuang **/private class TaskManagerAdapter extends BaseAdapter {private LayoutInflater mInflater; private List
 
  
Infos; public void setInfos (List
  
   
Infos) {this. infos = infos;} public TaskManagerAdapter () {mInflater = getLayoutInflater () ;}@ Overridepublic int getCount () {return infos. size () ;}@ Overridepublic Object getItem (int position) {return infos. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View view = null; viewHolder holder = null; If (convertView! = Null) {view = convertView; holder = (ViewHolder) view. getTag ();} else {view = mInflater. inflate (R. layout. task_manager_item, null); holder = new ViewHolder (); holder. iv_task_manager_icon = (ImageView) view. findViewById (R. id. iv_task_manager_icon); holder. iv_task_manager_name = (TextView) view. findViewById (R. id. TV _task_manager_name); holder. iv_task_manager_memory = (TextView) view. findViewById (R. id. TV _task_manager_memory); // obtain the CheckBox control holder on the UI. cb_task_manager_selected = (CheckBox) view. findViewById (R. id. cb_task_manager_selected); view. setTag (holder);} TaskInfo taskInfo = infos. get (position); holder. iv_task_manager_icon.setImageDrawable (taskInfo. getTask_icon (); holder. iv_task_manager_memory.setText (occupied memory: + TextFormat. formatByte (taskInfo. getTask_memory () x 1024); holder. iv_task_manager_name.setText (taskInfo. getTask_name (); String packageName = taskInfo. getPackageName (); // The application is the currently running program if (packageName. equals (getPackageName () {holder. cb_task_manager_selected.setVisibility (View. GONE);} else {holder. cb_task_manager_selected.setVisibility (View. VISIBLE);} // obtain the selected state of the entry boolean isChecked = taskInfo. isChecked (); if (isChecked) {holder. cb_task_manager_selected.setChecked (true);} else {holder. cb_task_manager_selected.setChecked (false) ;}return view ;}}
  
 

 

2. Update Handler

Here, because we have updated the custom adapter class and the data setting method is changed, we also need to update the display method of the default process list, after Handler instantiates the custom adapter class TaskManagerAdapter, We need to manually set the process list to the list attribute field in this class, and then call the notifyDataSetChanged () method of ListView to refresh the list display.

The Code is as follows:

 

Private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case SUCCESS_GETTASKINFO: long total = TaskUtils. getAvailMem (TaskManagerActivity. this); for (TaskInfo info: taskInfos) {total + = info. getTask_memory () * 1024;} // available memory String availMemStr = TextFormat. formatByte (TaskUtils. getAvailMem (TaskManagerActivity. this); // total memory String totalMemStr = TextFormat. formatByte (total); TV _task_manager_task_memory.setText (available/total memory: + availMemStr +/+ totalMemStr); mAdapter = new TaskManagerAdapter (); mAdapter. setInfos (taskInfos); rl_loading.setVisibility (View. GONE); lv_taskmanage.setAdapter (mAdapter); break; default: break ;}};};

 

3. Update click events for "one-click cleanup"

In this method, we create a new list set to store processes that are not killed. After processing, set the unkilled process set to the list set of the custom adapter class TaskManagerAdapter, and then call the notifyDataSetChanged () method of ListView to refresh the list.

Note: When traversing a set, you cannot add, delete, or modify the set.

The Code is as follows:

 

/*** Kill_process (View v) @ param v */public void kill_process (View v) {// store the List of processes not killed
 
  
NewTaskInfos = new ArrayList
  
   
(); For (TaskInfo taskInfo: taskInfos) {if (taskInfo. isChecked () {// kill the selected process am. killBackgroundProcesses (taskInfo. getPackageName ();} else {newTaskInfos. add (taskInfo) ;}} mAdapter. setInfos (newTaskInfos); mAdapter. notifyDataSetChanged ();}
  
 

 

Iii. Running Effect

4. Tips

In this example, for the purpose of writing some text directly in the layout file and related classes, you need to write these words in the real project. in xml files, reference these resources externally. Remember, this is the most basic development knowledge and specification for an Android programmer. I am writing these resources in the class and layout files for convenience.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.