Problems in getting the list of running apps - android

I need to get the list of running apps(apps that you see when you click on "recent apps" button). Below is the function I'm using to check whether an app running in background
boolean isNamedProcessRunning(String packageName){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
Log.e("", "---> running "+runningAppProcessInfo.get(i).processName);
if(runningAppProcessInfo.get(i).processName.equals(packageName)) {
return true;
}
}
return false;
}
Everything seemed to be fine until I tried "Chrome" app. For instance, I opened chrome and closed by using home button. And I could see "Chrome" on "app history" / "running apps" list. And I swiped away chrome from there. Still the above code says "Chrome" is running on background. How can I solve this?

KillProcess only can kill that process. otherwise it will be listed in getRunningAppProcesses()
Please check this answer
https://stackoverflow.com/a/11865184/3020568

Related

As a system app, kill another process, by package name

I'm developing a watchdog system service on android.
In case I detect that some foreground app is blocked/halted I want to force restart of the app.
I've already tried kill background app, with no success (as i'm trying to kill a foreground app).
also, via exec(adb shell su killall com.package.name) crashes with no permission, as i'm "just" system, not rooted device.
Finally got a way.
You can get the pid name by
private int getAppUid(String packageName){
ActivityManager mActivityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pidsTask = mActivityManager.getRunningAppProcesses();
for(int i = 0; i < pidsTask.size(); i++) {
if (pidsTask.get(i).processName.equals(packageName)){
return pidsTask.get(i).uid;
}
}
return -1;
}
after that, you can just kill it with
android.os.Process.killProcess(pUID);
works for my android 7 device.

How to check if my app is still running in an Android service?

I'm starting to develop an Android sticky service for my app with Delphi 10.1 Berlin, but I haven't found any tutorial or book that get into it, so I'm asking:
Which is the simplest way to detect if my app is still running or has been killed by the OS/user?
You can look for the process id by package name and see if it is still active.
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for (int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equalsIgnoreCase("packageNameSearchingFor")) {
processid = info.pid; //found it, we are running
}
}
Or you can simply store a shared value in public shared pref or database accessible through ContentProvider that is updated when in foreground or background to check. Either way is fine.

How some apps still can get current apps processes and kill them?

Background
In the past, I've found the next method of killing an app's background processes, given its package name:
public static boolean killApp(final Context context, final String packageName) {
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
for (int i = 0; i < pids.size(); i++) {
final ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equals(packageName)) {
android.os.Process.killProcess(info.pid);
if (new File("/system/bin/kill").exists()) {
InputStream inputStream = null;
try {
inputStream = Runtime.getRuntime().exec("kill -9 " + info.pid).getInputStream();
final byte[] buffer = new byte[100];
inputStream.read(buffer);
} catch (final IOException e) {
}
StreamsUtil.closeStream(inputStream);
}
am.killBackgroundProcesses(packageName);
return true;
}
}
am.killBackgroundProcesses(packageName);
return false;
}
The problem
Ever since a specific Android version (5.1), the function to get the list of running processes only returns the current app's processes, so it's quite useless to use it.
What I've found
Most apps on the Play Store indeed fail to show a list of processes, and instead, show just the current app's process or a list of services at most.
It seems that some apps still manage to show background apps processes and even be able to kill them. As an example, I've found AVG's app that's capable of doing so, here .
Before they can do it, they tell the user to enable the usage stats settings for the app, which I remember of using for checking general information of apps launch time.
Another app that succeeded killing background processes, yet without any user confirmation , is "fast task killer". It also shows a toast of all processes being killed. I could be wrong, but it seems that it's always the same number of tasks.
I also think there is a relatively easy way to get the list of processes using the "ps" function, but only if the device is rooted (otherwise it will return just the current app's processes).
There was a temporary solution with a library, found here (published here), but this doesn't seem to work on Android 7.1.2 , and most probably on previous versions.
The question
How do apps get the list of apps that have background processes, and how do they kill them?
Is it possible to do so without using the UsageStatsManager class ?

Stop background apps permanently Android

I am curious how apps like Task Managers & a specific app named Purify are able to permanently stop(force stop) background apps without requiring root permissions.
I searched & found a few ways but they were not efficient enough to stop all apps.
First method I found was to use KILLBACKGROUNDPROCESSES function but it was not able to kill all the background apps.
Secondly, I read about the SU FORCE STOP method but it requires root.
How are the apps able to do it without root?
Some Android applications can close other processes that are currently running in the background, not all process can be terminated. Some process even if terminated are restarted immediately. (You can try to close some from the 'application manager' and see for yourself).
You could try the following code that i compiled, it allows you to specify which processes that you may not want to kill.
// List of packages not to kill
List<String> reservedPackages = new ArrayList<String>();
reservedPackages.add("system");
reservedPackages.add("com.android.launcher2");
reservedPackages.add("com.android.inputmethod.latin");
reservedPackages.add("com.android.phone");
reservedPackages.add("com.android.wallpaper");
reservedPackages.add("com.google.process.gapps");
reservedPackages.add("android.process.acore");
reservedPackages.add("android.process.media");
//kill all except that in the list above
int processKillCounter = 0;
ActivityManager am = (ActivityManager) this.getBaseContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listeProcessus = am.getRunningAppProcesses();
for(ActivityManager.RunningAppProcessInfo processus : listeProcessus) {
String packageName = processus.processName.split(":")[0];
if (!this.getBaseContext().getPackageName().equals(packageName) && !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
processKillCounter++;
}
}
mi = new ActivityManager.MemoryInfo();
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long afterBoostMemory = mi.availMem / 1048576L;
long boostedMemory = afterBoostMemory - availableMegs;
//Percentage can be calculated for API 16+
long percentAvail = mi.availMem / mi.totalMem;
Toast.makeText(this,"Phone Boosted, "+boostedMemory + " MB freed", Toast.LENGTH_LONG).show();

Open and close 3rd party app within my app in android

I know it is impossible to close a running 3rd party app.
But what if i opened the 3rd party app within my app? Is there a way to close/finish it?
Yes it is possible but the user should have that app installed on his device. see following links
Open another application from your own (intent)
Launch an application from another application on Android
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for(int i = 0; i < pids.size(); i++)
{
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if(info.processName.equalsIgnoreCase("here your package name")){
processid = info.pid;
}
}
android.os.Process.killProcess(processid);
Hope it will help :)

Categories

Resources