Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Android - List and Kill Background Process

4.29/5 (6 votes)
28 Oct 2015CPOL 40.9K   2K  
Android OS is a multitasking operating, there are a lot of running background process, these process make your device will be slower.

Introduction

Android OS is a multitasking operating system, there are a lot of running background process, these process make your device slower. Android SDK provides a set of API that allow a developer to list all background processes and kill (stop) them. In this post, we will discuss about how to list and kill background process in Android device.

Background

Known android App GUI and thread

Code

We need two permissions - KILL_BACKGROUND_PROCESSES and  GET_TASKS.
Declare two permission as:

XML
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.GET_TASKS" />

Declare 2 variables:

Java
List<ActivityManager.RunningAppProcessInfo> processes;
ActivityManager amg;

Register service with Android to get all running processes:

Java
// using Activity service to list all process
amg = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
// list all running process
processes = amg.getRunningAppProcesses();

Create MyAdapter class extends BaseAdapter class to populate process's information on ListView as:

Java
public class MyAdapter extends BaseAdapter {

    List<ActivityManager.RunningAppProcessInfo> processes;
    Context context;

    public MyAdapter(List<ActivityManager.RunningAppProcessInfo> 
    	processes, Context context) {
        this.context = context;
        this.processes = processes;
    }

    @Override
    public int getCount() {
        return processes.size();
    }

    @Override
    public Object getItem(int position) {
        return processes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return processes.get(position).pid;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Process pro;

        if(convertView == null)
        {
            convertView = new TextView(context);
            pro = new Process();
            pro.name = (TextView)convertView;

            convertView.setTag(pro);
        }else
        {
            pro = (Process)convertView.getTag();
        }

        pro.name.setText(processes.get(position).processName);

        return convertView;
    }

    class Process
    {
        public TextView name;
    }
}

Display list of process on listview, display name only (just demo).

Java
adp = new MyAdapter(processes, MainActivity.this);
lst.setAdapter(adp);

When user longclicks on process name, this process will be killed:

Java
lst.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> 
    	parent, View view, int position, long id) {
        if (load == 1) {
            for (ActivityManager.RunningAppProcessInfo info : processes) {
                if (info.processName.equalsIgnoreCase(
                        ((ActivityManager.RunningAppProcessInfo)parent.getItemAtPosition
				(position)).processName)) {
                    // kill selected process
                    android.os.Process.killProcess(info.pid);
                    android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL);
                    amg.killBackgroundProcesses(info.processName);
                }
            }
            load = 0;
            // refresh list of process
            skill_Load_Process();
        }
        return true;
    }
});

You can only kill user process, with system process you need a rooted device.

Points of Interest

This is a very simple tip for killing running background processes on Android.

History

  • Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)