1

I have an Android Phonegap (3.0) application that need to periodically poll a web API for data.
I would like to keep it alive, even when it is moved to the background. I followed the general advice in this post and implemented a WakeLock with PARTIAL_WAKE_LOCK level, which is being created in the application's onCreate method

      pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
      wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, org.apache.cordova.CordovaActivity.TAG);

      wl.acquire();

I also have a call to wl.release() in onTerminate()

The behavior I see (with logging) is that the app will keep running (polling server data periodically) even when the screen goes dark. But when I switch to another forgraound app, the polling will stop withon a few minutes.

Update: I do have the required permission set in the manifest file

<uses-permission android:name="android.permission.WAKE_LOCK" />
Community
  • 1
  • 1
Ron Harlev
  • 16,227
  • 24
  • 89
  • 132
  • 1
    I'm under the impression wakelocks are intended for keeping the device active while your app is running in the foreground, not to keep it alive for any length of time while it's in the background and other apps are in the foreground. A background service may be a way of keeping your app alive in the background for long periods (and use less battery than a wakelock) – DaveAlden Aug 31 '13 at 08:30

2 Answers2

0

Do you have the required permission set in your Manifest?

<uses-permission android:name="android.permission.WAKE_LOCK" />

Phonegap plugin Powermanagement

Amit Prajapati
  • 13,525
  • 8
  • 62
  • 84
  • Yes. Forgot to mention that – Ron Harlev Aug 30 '13 at 20:31
  • Hi @RonHarlev were you able to achieve the results. I am also trying to make my application appear when my phone is in sleep mode. I need to wake up the phone when an event occurs. I tried wakelock but it does not wake up the phone. – Zeeshan Jan Jul 13 '15 at 04:40
0

It seems that above plugin doesn't exists any more. You can use this one instead: Keep screen on - PhoneGap 3.0+ plugin

maxxx
  • 657
  • 7
  • 5