Android – List and kill background process


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

List all android background processes
List all android background processes

We need two permission are KILL_BACKGROUND_PROCESSES and to GET_TASKS.
Declare two permission as

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

Declare 2 variable

List<ActivityManager.RunningAppProcessInfo> processes;
ActivityManager amg;

Register service with Android get all running processes

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

Create MyAdapter class extend BaseAdapter class to populate process’s information on ListView as

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).

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

When user longclick on process name, this process will be kill

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 only kill user process, with system process you need a rooted device.

Ok, download sourcecode Here

Advertisements

Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s