Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill currently running task in android

Tags:

People also ask

How do you end tasks on mobile?

You can open Task Manager on Android smartphones in the multitasking panel button by the right/left side of your phone's Home button. To exit any app, simply use the Task manager icon by pressing the multitasking button and then tap on X beside the specific app you want to close.

How can I kill other apps from my Android app?

In various older versions of Android, you can tap Settings > Apps or Settings > Applications > Application manager, and tap on an app and tap Force stop. In Android 10, the sequence is Settings > Apps & notifications > App info > [App name] > Disable or Force stop.


I am trying to build a task killer type of app in android. I can show the list of currently running task using ActivityManager but facing problem in killing the task.

Here is what i am doing to get the list of currently running task :

ActivityManager am = (ActivityManager) context.
            getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> a = am.getRunningTasks(Integer.MAX_VALUE);

PackageManager pack = this.getPackageManager();

for(int i = 0; i < a.size(); i++) {
    String packageName = a.get(i).topActivity.getPackageName();
    Drawable d = null;
    String appName = "";

    try {
        d = pack.getApplicationIcon(packageName);
        appName = (String)pack.getApplicationLabel(pack.getApplicationInfo(packageName,PackageManager.GET_META_DATA));
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    
    packName.add(packageName);  //arraylist of package name
    apps.add(appName);          // arraylist of app name
    icons.add(d);               //arraylist of icons
}

It worked for me But now as I am trying to kill the task using killBackgroundProcesses :

am.killBackgroundProcesses(package name);

It did not work . Also killBackgroundProcesses method is available in API level 2.2 then how can i perform the same task in previous API level such as 1.6 etc.

Is there any other way to show the currently active apps and kill them?