'How can I kill an app that I have started earlier in Android?
My app executes other apps depending on different events. For example, if the Bluetooth gets connected to "my loudspeaker" then my app launches Spotify. This works correctly.
Is there a way to kill the started app by code? for example when the Bluetooth gets disconnected from "my loudspeaker".
I have read a lot of questions about killing apps or background processes but none seem to work nowadays. Is there any "dangerous" permission I can ask to do this? Is there any really working way of doing this?
The good part in my issue is that I started the app from my own app, so maybe there is a way to get a process id or something to be able to kill it?
My code to start an app is this:
Intent LaunchApp = getPackageManager().getLaunchIntentForPackage("the.other.package");
startActivity(LaunchApp);
To kill it I have tried with this:
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses("the.other.package");
and
final List<ActivityManager.RunningAppProcessInfo> runningProcesses = oActivityManager.getRunningAppProcesses();
for(ActivityManager.RunningAppProcessInfo runningProcess : runningProcesses) {
if(runningProcess.processName.equals("the.other.package")) {
android.os.Process.sendSignal(runningProcess.pid, android.os.Process.SIGNAL_KILL);
}
}
and this:
Method forceStopPackage = oActivityManager.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(oActivityManager, "the.other.package");
But nothing works!
How do "Task Killer" apps work? Can I ask my app to be an app administrator or something?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
