0

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2055727
  • 1
  • 1
  • 3

2 Answers2

1

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.

Carp Fisherman
  • 141
  • 1
  • 3
  • 17
0

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)

Richard Lindhout
  • 2,038
  • 2
  • 23
  • 38
  • this is not a bug. Most of the apps have `broadcastReceivers` which are restarted automatically by the Android. Consider that android allowed you to kill all the `broadcastReceivers` without restarting. Then the important receivers such as TIME_TICK ,BATTERY_LOW receivers will also be killed and that would create a havoc. BroadcastReceivers are very important and have important tasks so are never killed by android. – Rohan Kandwal Jul 11 '13 at 20:19