user1151566
user1151566

Reputation: 43

Phonegap android application cpu usage in background

I have been doing a phonegap app for android but am stuck on the following; When I background the app and check the task magager, the app still consumes a considerable amount of CPU (between 5% and sometimes even to 15%). I have tried removing all other parts of my code but the only thing that stops the app from having CPU activity in the background is removing the phonegap.js from my code.

I was under the impression that Phonegap would halt javascript execution when going into onPause but I must be missing something.

I have tried 1.1.0, 1.2.0 & 1.3.0 to no avail. Interesting too is that if I fire up 2 apps with phonegap, their background CPU usage seem to behave the same: One goes up, the other goes up and mostly they are exact in usage up to the percentage point.

Has anyone got any idea what the app is still doing in CPU and/or how I could find out ?

Regards

Upvotes: 4

Views: 1455

Answers (3)

Don
Don

Reputation: 659

I noticed the same problem, especially on KitKat. To resolve the issue, in your main activity (the activity which extends DroidGap), you need to call the onPause() method for your WebView (which is the 'appView' variable).

This pauses any extra processing associated with the WebView which should reduce your CPU usage significantly:

@Override
protected void onPause()
{
    super.onPause();

    if (appView != null)
    {
        appView.onPause();
    }
}

@Override
protected void onResume()
{
    super.onResume();

    if (appView != null)
    {
        appView.onResume();
    }
}

Upvotes: 0

Ostkontentitan
Ostkontentitan

Reputation: 7010

Cordova/PhoneGaps default behaivior is to keep the app running in background, you can change that by adding a "keepRunning" tag to your config.xml:

keepRunning (boolean, defaults to true) - Determines whether Cordova will keep running in the background or not

<preference name="keeprunning" value="false"/>

Upvotes: 1

ghostCoder
ghostCoder

Reputation: 7655

the js execution is not halted when the app goes into onPause, it only halts when the app is closed/killed.

Upvotes: 2

Related Questions