Hi im developing an android application and im wondering how i make the app use less CPU while backgrounded. For example, my application pauses once it loses focus but the cpu usage is still 50%
Edit
while (!pause) {
Canvas c = null;
try {
c = sHolder.lockCanvas(null);
synchronized (sHolder) {
doDraw(c);
powerUps();
}
} finally {
if (c != null) {
sHolder.unlockCanvasAndPost(c);
}
}
}
Then when you pause and resume it just changes the pause variable
IMO, you should not be looping at all while your app is paused.
See this page for more details about the “proper” lifecycle of an android app.
Also note that with any busy loop design your app will be using 100% of the CPU – I’m not sure if starting a loop from the
onResume()method is correct; my instinct is to have each iteration of your loop performed by a separate call initiated by a call toHandler.post(), with the last statement re-posting the next call. Otherwise, the application’s main thread will be starved, right?Maybe something like this: