Destroying running app process

I was trying to create a process killing extension So whole code is here

 import android.app.Activity;
 import android.app.ActivityManager;
 import com.google.appinventor.components.annotations.SimpleFunction;
 import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
 import com.google.appinventor.components.runtime.ComponentContainer;
 import com.google.appinventor.components.runtime.util.YailList;
 import java.util.ArrayList;
 import java.util.List;

 public class KillTask extends AndroidNonvisibleComponent {
     private Activity activity;

public KillTask(ComponentContainer componentContainer) {
    super(componentContainer.$form());
    this.activity = componentContainer.$context();
}

@SimpleFunction
public YailList KillTask() {
    ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runAppList = activityManager.getRunningAppProcesses();
 
int listsize = runAppList.size();
	
if (runAppList != null) {
 		for (int i = 0; i < listsize; ++i) {
     		if (runAppList.get(i).pid != android.os.Process.myPid()) {
			android.os.Process.killProcess(runAppList.get(i).pid);
			activityManager.killBackgroundProcesses(runAppList.get(i).processName);
		}
	}
}    }

Note : this java code is written by my senior java teacher for killing process but sir told me he also don't know about making extension for MIT app inventor and he said "ask someone to adjust code the for extension " and i am having one AndroidRuntime jar file for this

I am also from different country and my english is not prefect.

so anyone can adjust the code and make an aix file ?

edit: Updated code by my sir

any help

The main error is you've not returned any YailList.

maybe it should be public void ?

If you don't want any thing to be returned then it should be void.. Wait i will compile it..

2 Likes
@SimpleFunction
public void KillTask() {
ActivityManager activityManager = (ActivityManager)(this.context).getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runAppList = activityManager.getRunningAppProcesses();
 
int listsize = runAppList.size();
	
if (runAppList != null) {
for (int i = 0; i < listsize; ++i) {
if (runAppList.get(i).pid != android.os.Process.myPid()) {
android.os.Process.killProcess(runAppList.get(i).pid);
activityManager.killBackgroundProcesses(runAppList.get(i).processName);
}
}
}    
}

I think this will solve this .
Extension : Test.aix (5.2 KB)

2 Likes

thank you so much :relaxed: :relaxed:

edit: not working i am trying to fix it maybe permission needed

2 Likes

I've just fixed the error,
I think it need this permission ,

`android.permission.KILL_BACKGROUND_PROCESSES`

Use @UsesPermissions(permissionNames="android.permission.KILL_BACKGROUND_PROCESSES") to fix the error.
1 Like

If this is required permission then here is extension,
Test.aix (5.3 KB)

3 Likes