Optimal battery usage

As a programmer, what measures can I take to ensure that my application does not run a lot of resources and does not drain the battery?

+6
source share
1 answer

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

  • Do not use excessive network calls. Try to save a cache of data that will change infrequently, and just start a full update, say, 10 seconds after the last update (stops them from spamming on the server, gives a faster response).
  • Cancel any running asynchronous tasks if they are not needed (for example, you do not need to download the rest of this image / website if the user goes to an activity that uses it).
  • Use OnPause / OnResume to pause / resume games
  • Use the OnStop / OnStart methods to save the program state and restart it if necessary. Please note that in this state the application is "no longer visible" and can be killed if other applications require memory, which means that the next time you start, you will either go to onRestart () or onCreate ()
  • Do not leave the screen on ( setKeepScreenOn(boolean) or android:keepScreenOn ). the video should probably be one of the only instances in which you would use this functionality.
  • Avoid creating widgets that update frequently, and only refresh when it is visible.

There is a good flowchart showing the various methods called to pause / resume on the Android developer site:

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

+4
source

Source: https://habr.com/ru/post/913694/


All Articles