Can someone help me with sample code on retrieving the packages names of the activities that are currently running in Android?
Edited:
Code 1
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
if (processes != null){
for (int i=0; i<processes.size(); i++){
RunningAppProcessInfo temp = processes.get(i);
String pName = temp.processName;
for (int k=0; k<blacklist.size(); k++){
if (pName.equalsIgnoreCase((String)blacklist.get(k))){
int pid = android.os.Process.getUidForName(pName);
android.os.Process.killProcess(pid);
}
}
}
Code 2?
int pid = temp.pid;
String[] packages = temp.pkgList;
for (int j=0; j<packages.length; j++){
String packageName = packages[j];
for (int k=0; k<blacklist.size(); k++){
if (packageName.equalsIgnoreCase((String)blacklist.get(k))){
//Android 2.2+ kill all background processes
am.killBackgroundProcesses(packageName);
}
}
}
Assuming that black has the application package name of program which I have blacklisted and want to terminate. Which of the above code is the correct way in which to implement my task killer. Can someone explain the difference between killBackgroundProcesses(packageName) and android.os.Process.killProcess(pid) as I cant seem to get the difference between them.