How to kill other applications from a system application in Android - android

I have rooted my mobile and I have made my application as system application by granting the required permissions. Now, I have tried using:
activityManager.killBackgroundProcesses(packageName);
android.os.Process.killProcess(pid);
by giving the permissions:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
My application is still not able to close other applications. I have tried closing what's app from my application. The above code is getting executed without any errors. Though my application is a system application( I have tried my application as user application too), but still linux kernel is ignoring the call to kill other applications.
I know it is not good to kill other applications from our own applications, but I want to know what else should I need to do to kill other applications.

Note that,
android.os.Process.killProcess(pid);
allows the system 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;
You can try killing third party apps via command line:
adb shell am force-stop com.my.app.package
And of course you can run this command from java also.

Related

How to kill another Application or Activity programmatically without being root [duplicate]

This question already has answers here:
Kill another application on Android?
(2 answers)
Closed 3 years ago.
I want to kill another app from my Application once the other app comes into foreground.
I am already capturing if my app is in foreground or background. When my app gets to background, I want to filter for the right process by it's name and use its pid to kill it. I'm using ActivityManager.getRunningAppProcesses() and filtering the processes for the wanted name, but only processes related to my app are shown. That's the first problem, because I cannot access the pid of the other apps processes.
The next problem is that, even by using the current pid (from adb) of the other apps process, my code which should kill it does not work (the other app keeps running).
I am using
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> and <uses-permission android:name="android.permission.GET_TASKS" />.
I assume that I would need be root to kill other apps, so also something like Runtime.getRuntime().exec(arrayOf<String>("su", "-c", "kill 24043")) does not work.
val amg = getSystemService(ACTIVITY_SERVICE) as ActivityManager
var processes = amg.getRunningAppProcesses();
for (info in processes) {
if (info.processName.equals(otherAppName)) {
val process = Runtime.getRuntime().exec(arrayOf<String>("su", "-c", "kill $pid"))
android.os.Process.killProcess(info.pid)
android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL)
amg.killBackgroundProcesses(info.processName)
}
}
The last three lines result in the signal being send, but nothing happening and the one before obviously leads to java.io.IOException: Cannot run program "su": error=13, Permission denied.
Is there another way to kill another app, that doesn't require to be root?
You can't kill another app for security reasons. As a user, i don't want to install an app that close my apps without a good reason and without I even know about it, it would be pretty weird. That being said, it's possible to close background process from another app's, I didn't test it by myself but here is:
ActivityManager manager = (ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);
manager.killBackgroundProcesses(packageName);
You'll need to put this on your Manifest as well:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

Android: How to get the process id of the foreground app

First of all, please note that this question is not same as all the "android foreground app" questions I found on SO, please read on :-)
I'm trying to write an android app for my own use, using golang, without using android-sdk or ndk (this is the KEY point). It is pretty simple, just use golang to write a http service, compile it for arm CPU and voila my app is running and can be access by simply visit http://localhost.
For the purpose of my app, I need to know the currently running foreground application, to define it precisely:
Foreground application is the application that occupies the screen, or has an "activity" what-so-ever (forgive me I'm not an android developer).
Anything that that is depended by the foreground application (e.g. services) is NOT what I am interested in.
If the phone is LOCKED/screen turned off, I want the solution to tell me there is NO foreground app.
And since I do not use anything android, just treat the phone as a LINUX machine, I want the solution to use native LINUX ways, e.g. by inspect /proc, or by calling any installed android command line tool (including sending messages via these command line tools), but NOT using any SDK/NDK way so that I have to use java or incorporate these thing into my app.
Starting from Android SDK 26 (if I remember well) Apps are executed on -one-User-per-App, so (i.e.) WhatsApp is running on UID=30 and Telegram on UID=76, so executing a ROOT command of "ps -A -o PID,USER,NAME" you can parse output and then Kill all Processes that you don't want to be executed.
36119 u30_a149 <WhatsApp_packagename>
36203 u76_a211 <Telegram_packagename>
37399 root [kworker/1:2H]
37423 u0_a329 su
38069 root sh
Without Root Permissions nothing of what you're trying to achieve is possible simply because is not possibile to denied an application to be executed or to kill it without Superuser privilege.

monkeyrunner script - starting an activity that requires a permission

in a monkeyrunner script while launching an activity, is there a way to mimic yourself having a certain permission that the starting activity requires?
I am using "device.startActivity(component='com.package/.MyActivity)" but the activity MyActivity requires a permission, and hence device.startActivity fails. Is there a way to give this permission to the script?
When I had this problem, I solved it by creating a very small application(with the correct permissions in the manifest) that I pushed to the phone. All the application did was re-send intents sent to it, but to a different destination. My application also had a gui for triggering events manually, but that's optional.
You can add permissions in AndroidManifest.xml file.
I don't know what monkeyRunner script is, and do we talk about the same permissions here, but in Android, all permissions you want to give to the app, you go to Manifest file.
Running an activity through monkeyrunner is not exactly different than running it manually. So, when it asks for permission, you can verify it right after installation by sending an extra command like:
device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
or
device.press('KEYCODE_BUTTON_SELECT', MonkeyDevice.DOWN_AND_UP)
You can also get your application have system privilages by pushing it into a special folder with these commands:
>adb remount
>adb push your\local\apk\path.apk system/priv-app
>adb shell stop
>adb shell start
Hope it works for you...

Close a background android application

The requirement is to kill an application which is pushed to the background after a time interval. How can i do that in android? If anybody could share the code snippet, it would be great.
Thanks,
Jinu
The android framework does not provide permission to kill other app process.
You can only kill your app processes.
Process kill permission is only for system apps.
Still try using this permission in your Menifest
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

how to force close the currently running application in android

i am trying to find out the list of processes which is running in the device.
after that i want to force close all the process except my current application.
I tried using this below code . Its listing out the processed but its not killing the processes .
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> process = manager.getRunningAppProcesses();
for (int i = 1 ;i< process.size();i++)
{
int pid = process.get(i).pid;
System.out.println("Task " + process.get(i).processName);
android.os.Process.killProcess(pid);is
}
This above code only i tried to kill the processes. once after the killprocess called i again called the getRunningAppProcess() to check the process are killed or not. still its showing all the processes.
Its listing out the processed but its not killing the processes .
Of course. You do not have rights to kill other processes using killProcess(). I have no idea why you think you would have such a right.
We want to kill all foreground as well as background application also.
This will crash the operating system.
If you only want your application running, build your own devices with your own firmware and your own OS and your own GUI framework, put your application on those devices, and distribute those devices.
You can try killBackgroundProcesses() (it needs package names), you need the KILL_BACKGROUND_PROCESSES permission for it. And you can kill your own process without a special permission (be sure to do it last).
If it still doesn't work, you need to root the phone, and do it as root.

Categories

Resources