I use the following snippet to kill tasks , it works perfectly fine but it doesn't kill my own app. How do i make it kill my app as well?
// kill tasks
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");
ActivityManager am = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> listeProcessus = am
.getRunningAppProcesses();
for (RunningAppProcessInfo processus : listeProcessus) {
String packageName = processus.processName.split(":")[0];
if (!context.getPackageName().equals(packageName)
&& !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
}
}
Google has recently removed the GET_TASKS permission and developers can't make changes to running tasks . The only remained way is to have a root access and close the running tasks that is not possible for All Android Users
As of api 21 (android lolipop) it has been removed by Google
Related
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.
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();
Problem: I want to kill a background application process.
We call the below methods inside a background thread/services and it's not working.
We have tried a few methods available on net but not succeeding to kill the background process/application.
My Device has a root permisision already.
Code here
1st Method:
int value = findPIDbyPackageName("com.google.android.youtube");
android.os.Process.sendSignal(value, 9);
2nd Method:
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.google.android.youtube");
3rd Method:
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
activityManager.restartPackage("com.google.android.youtube");
4th Method :
android.os.Process.killProcess(pid);
5th Method : `
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue; // here my package defines your application package entered in manifest
else if(packageInfo.packageName.equals("third Party application Package Name")) // if you dont have this package name then prefer playstore url of this app to get packagename
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
6th
We launched a third party app like youtube, Subway surf from our own android app.
We are using startActivityForResultmethod for launching the app.
launchApp("com.imangi.templerun");
protected void launchApp(String packageName) {
mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
try {
startActivityForResult(mIntent, 101);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(this, "App not found", Toast.LENGTH_SHORT); t.show();
}
}
}
To close the application, the method finsihActivity(ResposneCode) is available.
But we're not able to use it in service.
You are not allowed to kill processes which don't belong to your app. The system will decide when to kill and what to kill when necessary.
App intervention is just to tell the system a message, Please help me to kill this process, blah blah..., that's all.
Process.killProcess() should work, this is your method number 4.
Read carefully what it says.
Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.
Under root permission you should be able to remove the kernel restrictions, unfortunately I do not know how to do this, I suggest you to search for native solution for that, may be some C codes, there are plenty of them in the net.
Also in your method 1 you been trying to send SIGNAL_KILL, try sending SIGNAL_QUIT
I'm trying to find some info about apps I find using shell's top command. All I have is a process name (containing package name). Icon and app name would be perfect. I can't find any suitable soultion via google. Any help would be aprreciated;)
To be preemptive, I use top because it's the only way I found to show current processor usage. If someone's familiar with some more API friendly soultion, I'd be grateful.
Example process names I get are:
com.android.deskclock for desktop clock
com.creativemobile.DragRacing for game Drag Racing
Here how you get details. Since you have the package name, you can use it to get the corresponding application name, version, and icon.
List<PackageInfo> packagess = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packagess.size();i++) {
PackageInfo pack = packagess.get(i);
if ((!getSysPackages) && (pack.versionName == null)) {
continue ;
}
//this is the application name
pack.applicationInfo.loadLabel(getPackageManager()).toString();
//this is the package name
pack.packageName;
//this is the version name
pack.versionName;
//this is the version code
pack.versionCode;
//this is the application icon
pack.applicationInfo.loadIcon(getPackageManager());
}
You can try out below code :
private String getTopActivity() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(30);
return ar.topActivity.getClassName().toString();
}
you have to give the permission of get task in the AndroidManifest.xml
I have a requirement in my project where in i have to kill process of 3rd party application.
As i know Android can have multiple application running at a time, so can i get all those Process
Ids some how...
This class will help you to kill processes:
http://developer.android.com/reference/android/os/Process.html
This gives you a list of currently running processes:
ActivityManager.getRunningAppProcesses();
The list contains ActivityManager.RunningAppProcessInfo objects which store the pid. You can then kill the processes via
Process.killProcess(pid);
You need the proper permissions to do that. Android should throw an exception if you try to kill a process without the proper permissions and tell you what permission you need.
check this way if it solves your purpose:
ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = context.getApplicationContext().getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
packs.get(pos).gids;
packs.get(pos).sharedUserId;
You can get information about all running processes using ActivityManager. Refer following code:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
for (RunningAppProcessInfo proc : list) {
Log.v("Proccess", proc.processName + " : " + proc.pid);
}
Multiple applications can be running in the same process, so it would be better to use RunninAppProcessInfo.pkgList over RunninAppProcessInfo.processName.