Can Android Kill A Foreground Activity?
Solution 1:
The Android processes and application lifecycle document states that:
foreground process
...
There will only ever be a few such processes in the system, and these will only be killed as a last resort if memory is so low that not even these processes can continue to run. Generally, at this point, the device has reached a memory paging state, so this action is required in order to keep the user interface responsive.
Which means your activity (and therefore process) can be killed but only under extreme memory conditions and as a last resort. Empty processes, background processes, service processes and visible processes will all be killed before your process will be killed so it's extremely unlikely this will ever happen but the possibility is there if leaving your application open will lead to system instability.
Solution 2:
Every process is assigned an OOM score. The score depends on the process class and usually there are other processes to kill before Android OOM killer starts considering foreground processes. But, there are other classes with higher priority than foreground and it is entirely possible that someone (vendor?) stuffed the device with so many memory-hungry persistent processes that OOM has to kill some of the foreground class. Though I would say it is highly unlikely. What does "dumpsys meminfo" say?
Solution 3:
Answer 1: Yes.
Answer 2: Explain better please.
Resume: You can get all running processes and kill those with the specified pid
Add 2 permissions to the manifest:
<uses-permissionandroid:name="android.permission.GET_TASKS" /><uses-permissionandroid:name="android.permission.RESTART_PACKAGES"/>
Later use this code to get PID and later kill process
ArrayList<Integer> pids = newArrayList<Integer>();
ActivityManagermanager= (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
{
if (pids.contains(process.pid))
{
// Ends the app
manager.restartPackage(process.processName);
}
}
Post a Comment for "Can Android Kill A Foreground Activity?"