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"/>
Related
I am verifying my location service compatibility with Android Q but I am a little unsure how my app is going to react since in my testing I have seen not difference when granting Background permission vs Only while app is running.
Coming off this statement from the Q migration documentation
An app is considered to be in the background unless one of its
activities is visible or the app is running a foreground service.
Since the location service is a foreground service does the difference in permission even matter in this case?
Well it's a bit of a tricky question.
When running location foreground service on Q you need "while app running" permission + to declare in the manifest this foreground service is of type location. Your app has no need of the background permission.
If your app do asks for background permission, the user can get suspicious and reject any location permission :( So it is not recommended to ask the user for permissions you don't need.
highly recommend to watch this video from the google IO: Updating Your Apps for Location Permission Changes in Android Q (Google I/O'19)
You can read more about t here: documantation
And you can see googles example project on GitHub for location updates on Q with foreground service here :LocationUpdatesForegroundService
You just need run foreground service instead of background. Otherwise your app will crash during background services start when there is no activity on the foreground
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" />
I want to running my service 24x7 continuesly I achieve this all most but some device service are stopped. when we click on recent button and click on clear memory in MI device at that time it will killed my service. I try lots of but not able to solve this problem.
Please help me!!
Did you make your service sticky?
try adding <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> in your AndroidManifest.xml file and check.
Well, hello..
I have a question, how would you correctly use this
"ActivityManager.killBackgroundProcesses(String packageName)"
I have tried to replace packagename with a package, for now let's call it com.example.package.
It doesn't seem to be compiling it as it pops up with errors (I am new to Android app developing)
Would appreciate any help! =)
You must have a special permission to kill background processes from your application. Add the following permission in your manifest.
"uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"
The above permission enables to kill any background process running for the package name provided in "ActivityManager.killBackgroundProcesses(String packageName)"
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.