I want to implement a button that kills all running apps and associated processes myself, but I cannot make it to work with some installed apps.
I tried
activityManager.restartPackage( packageName );
activityManager.killBackgroundProcesses( packageName ); (Which is the new version of restartPackage)
android.os.Process.killProcess( pid );
My issue is that some apps don't get killed and when a refresh the list of running apps, they still there.
I've read:
ActivityManager
Android: Killing (all) Foreground running App
Related
On my AndroidTV Emulator I have:
val am = this.getSystemService(ACTIVITY_SERVICE) as ActivityManager
am.runningAppProcesses?.forEach {
Logger.debug("XXX", "Running ${it.uid} , ${it.processName}, ${it.pid}")
}
I also start other 3rd party apps, but I dont kill them.
The Google Docs say:
Returns a list of application processes that are running on the
device.
for ActivityManager.runningAppProcesses.
When executing this code from Android Studio, runningAppProcesses list has always size 1 and its the current app triggered from Android Studio?
I was expecting, that these 3rd party apps, I launched earlier are listed there as well?
What is meant by "application processes that are running on the device"? Are backgroundprocesses not included there?
How can I do so?
my app has a proxy server in a native executable written by C, it always runs in the background, let's call it httpproxy.exe.
The main activity I use startForegroundService and startForeground with a persist notification to keep the app in the foreground avoid killing by system, then run the httpproxy.exe in the background.
my app works well on android 5 and older verisons, but on android 8.0, when the activity goes background after minutes, httpproxy.exe looks like getting suspended, so I have to bring my app up time after time, that's unacceptable.
I heard there are a so-called Background Execution Limits on android 8,
how can I have my app normally work like before?
Make sure, that your app is excluded from the Doze mode.
Checking:
PowerManager powerManager =
(PowerManager)getActivity().getSystemService(Activity.POWER_SERVICE);
powerManager.isIgnoringBatteryOptimizations(getActivity().getPackageName());
Asking the user to white-list your app:
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);
This question already has answers here:
Listen to own application uninstall event on Android
(3 answers)
Closed 7 years ago.
Recently i've seen a funny app - Photo Wonder.
When this app is uninstalled, it shows a web survey page asking for the reason of app uninstall. Now, here is the problem.
As far as I know, after an app has been removed, the system broadcasts ACTION_PAKAGE_REMOVED intent.
But this funny app was able to show my the web page although the official doc says
"The package that is being installed does not receive this Intent."
Anyhow, I could find a process checking some kind of status of the app.
Now here is the question. Can the native app catch the broadcasted intent from android system?
If it is possible, please let me know how! :-(
I believe I've got the main idea of how they did it. Here is the pieces of the puzzle.
Any Android application can start a process by calling Runtime.exec() function.
Runtime.getRuntime().exec("chmod 755 '/data/data/my.app/files'/native_code");
After this line of code gets executed there is another process spawned. This process runs under the same linux user as the application itself.
When a user opens Settings -> Apps -> My App and presses "Force stop" button, main application process gets killed, but the process hosting native program (see above) still runs. I personally believe this is a security issue and I am going to report it back to AOSP.
Such native program can run infinitely and do nothing - just sleeping. But before going to sleep, it registers a termination signal handler which will be called when process is about to be terminated by the system.
int main(void) {
signal(SIGTERM, termination_handler);
while(1) {
sleep(10);
}
}
void termination_handler(int sig) {
// handle termination signal here
}
Now you should already know what the last piece is, right? My native termination_handler should be able to launch a browser. I didn't try this in code, but I assume this is possible, because I can do it using adb shell as following
adb shell am start -a android.intent.action.VIEW -d http://www.google.com
Now back to the question about how Dolphin Browser does it. Install the app and launch it at least once. Once started, it registers a native uninstall watcher using the principles described above. To see it, connect to the device and open adb shell. Then call ps to see list of processes. You will see two processes similar to following
u0_a109 315 ... mobi.mgeek.TunnyBrowser
u0_a109 371 ... /data/data/mobi.mgeek.TunnyBrowser/files/watch_server
As you can see it starts a watch_server native program, which is a part of its apk-file. Now open App info page of Dolphin Browser and press "Force Stop". Switch back to terminal and call ps again. You will see there is no mobi.mgeek.TunnyBrowser process anymore, but watch_server still runs.
By the way this approach will only work, if watcher server runs all the time. To
make sure it is always up, both apps require "run at startup"
permission, where they start their watchers.
Now, when you uninstall the app, Android stops all processes belonging to this application. Watcher receives termination signal and opens browser with predefined URL and then shuts down.
I might look a bit different in some details, but the main concept behind this hack must be as described.
There could be a tricky thing like that application is also having watcher service.
You can check the permission used by that app may contain INSTALL and UNINSTALL permissions.
HOW IT WORKS:
instead of single app that may have 2 app bundle.
as and when you install it, this app is also installing some service that is watching your app status
When you try to uninstall that app the system broadcast is called which is handled by that service and will check that if your package is exist in installed application or not.
as soon as this service finds that your package is not in the list it calls an intent with action view with the web url to open the brawser.
I am writing an Andoid app so that when battery life gets below a certain level, a dialog with options of how to save the battery appears. One of those options is to close all background apps/services (processes) using ActivityManager.killBackgroundProcesses(). The code is shown here:
public void TaskKiller( View view){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
}
However, when I click the button that calls TaskKiller() and closes the background processes, some of the apps (Email, Google Maps) instantly begin he process of restarting. How can I alter my code so these apps stay closed until they are reopened? Also, is this approach sensible in regard to saving power or am I attacking this the wrong way?
I don't think that's the right way of handeling the problem.
These apps have broadcast receivers, which mean they'll restart the service whenever something happens (i.e. AC plugged in/WiFi turned on), and I don't think there's a way to stop that without root, and actually disabling the broadcast receiver.
You could make something that kills it every 5 minutes, but that wouldn't be very battery-friendly.
I don't think it's a good idea to force close the Maps app everytime, it's a bug in Android i think..
One of the answers is as following:
"
Actually, Maps always runs when you have "Backround Data" checkmarked in your General Sync Settings under Account Settings in your phone's Gmail app. Syncing backround data is necessary, unfortunately, in order for your phone service provider to provide calling and texting (although internet access will still work without this item checkmarked). Unchecking this box will remove Maps from Running applications (& any other app that needs it), improving battery time and speeding up your phone. But, if you want to make calls, text or use apps that require Backround sync, you have to have this ckeckmarked. If all you want to do is browse the net...uncheckmark it. There are currently no other legitimate solutions to the issue. Hope this is helpful...
"
See this issue (https://code.google.com/p/android/issues/detail?id=10251)
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.