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.
using this in a loop,you will get package name of all currently running app ,to get running app name, see this EXAMPLE
first of you API greater then 20 then use the bellow code to definitely work. I use this code and work properly. Try it
You can use the getRunningAppProcesses on the Android ActivityManager to retrieve a list of running processes.
Check this link : http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses%28%29
You can use the getInstalledPackages method on the Android PackageManager to retrieve a list of all the installed apps
Check this link : http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledPackages%28int%29