Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal usage of battery

Tags:

android

As a programmer what are the measures I can take to take care that my app does not hog a lot of resources and drain the battery ?

like image 756
user669444 Avatar asked Apr 20 '12 05:04

user669444


1 Answers

Depending on the application you are writing, some of these may apply to you:

  • Don't use excessive network calls. Try to maintain a cache of data that will change infrequently, and only run a full refresh after say 10 seconds of their last refresh (stops them spamming the server, gives a faster response)
  • Cancel any async tasks running if they are not needed (for example, no need downloading the rest of that picture/website if the user navigates out of the activity that uses it)
  • Make use of OnPause/OnResume to pause/resume games
  • Make use of the OnStop/OnStart methods to save program state and reload it when necessary. Note that in this state, an app is 'no longer visible' and may be killed if other apps require the memory, meaning the next time it is run, you'll either go into onRestart() or onCreate()
  • Avoid setting the screen to remain on (setKeepScreenOn(boolean) or android:keepScreenOn) . video should probably be one of the only instances where you would utilise this functionality
  • Avoid building widgets that update frequently and only update it when it's visible

There is a nice flowchart showing the different methods that get called for pausing/resuming on the android developer site:

http://developer.android.com/reference/android/app/Activity.html

like image 152
Robotnik Avatar answered Oct 23 '22 17:10

Robotnik