Click here to Skip to main content
15,117,830 members
Articles / Mobile Apps / Android
Tip/Trick
Posted 28 Oct 2015

Tagged as

Stats

34K views
1.9K downloads
4 bookmarked

Android - List and Kill Background Process

Rate me:
Please Sign up or sign in to vote.
4.29/5 (6 votes)
28 Oct 2015CPOL
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)

Share

About the Author

Ngo Tuong Dan
Team Leader Tae Kwang Can Tho LTD
Vietnam Vietnam
Code for fun -> ^.^ <-

Comments and Discussions

 
PraiseGood example code Pin
Member 1376026221-May-18 10:47
MemberMember 1376026221-May-18 10:47 
GeneralFine on Android 4.0.3 - 4.4.2 Pin
Member 1376026221-May-18 10:53
MemberMember 1376026221-May-18 10:53 
Questionprocesses = amg.getRunningAppProcesses(); Pin
catalystOne Dupinder1-Jun-16 6:28
MembercatalystOne Dupinder1-Jun-16 6:28 
AnswerRe: processes = amg.getRunningAppProcesses(); Pin
Member 60234608-Mar-18 23:42
MemberMember 60234608-Mar-18 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.