5

I need to do background GPS tracking on an Android phone, and I'm currently planning on having two scenarios:

  1. When the phone is connected to a (car) charger, get frequent GPS locations (every minute).
  2. When the phone is not connected to a charger, only poll for a location once every 5-30 minutes, depending on how much the battery is affected.

I know that I'll need to implement the tracking in a service, and probably show a notification to keep the service alive. However, I gather that the phone might go into sleep mode at any time, which would stop the GPS tracking.

For these scenarios, what is the best way to do continuous background GPS tracking, without the phone going to sleep, and without draining too much power?

So far I gathered the following:

For scenario 1, I'll probably need a acquire a WakeLock, and keep the CPU awake all the time (but the screen may sleep). The MyTracks application seems to be doing this in TrackRecordingService. OpenGPSTracking seems to do the same.

For scenario 2, I can use AlarmManager to schedule a check every x minutes. My service will need to acquire a temporary WakeLock, wait for a single GPS location, then stop and release the WakeLock again.

How would the power usage compare for these two methods (permanent WakeLock vs AlarmManager)? Can the GPS receive an accurate location in a reasonable amount of time when only checking every 30 minutes?

There are a few related questions on StackOverflow, but none with satisfying answers. Some semi-useful ones:

Community
  • 1
  • 1
Ralf
  • 14,655
  • 9
  • 48
  • 58

2 Answers2

7

You can use broadcast receiver if the power is connected or not and do your action accordingly.

For Power Connected:

public class PowerConnected extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_CONNECTED")){
      //Initialize your listener here...
   }
   }

}

In your manifest file:

<receiver android:name=".receiver.PowerConnected" >
<intent-filter >
     <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>

When Power is Disconnected:

public class PowerDisconnected extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_DISCONNECTED")){
    //Do your task here...      
  }
 }
}

In your manifest file:

<receiver android:name=".receiver.PowerDisconnected" >
<intent-filter >
   <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

Now you should use service and initialize your GPS there. When power is connected use GPS_Provider else use network provider.Use wakelock if needed.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • 2
    While I'll definitely use this, I feel it doesn't answer my core question on what the best strategy is for GPS tracking. – Ralf Oct 14 '11 at 13:52
  • You shouldn't have to choose between using GPS_Provider or network provider. You can include both and let Android pick the one automatically that is available. – Johann Apr 19 '13 at 04:53
5
  1. When the phone is connected to the charger u can acquire the wakelock as u a planning to get frequent GPS locations, AlarmManager doesnt seems to be an great idea in this case.

  2. When phone is not connected to the charger u may use alarm manager as u ll only poll for a location once every 5-30 minutes. Acquiring wakelock will affect ur battery life significantly.

Implementation is up to u.

Cheers....!!

Rohit
  • 593
  • 2
  • 8
  • Any idea how much the wakelock affects the battery? And how long will it take to acquire a location if it's only done once every few minutes? – Ralf Oct 14 '11 at 11:58
  • Device battery life will be significantly affected by the use of this API. Do not acquire WakeLocks unless you really need them, use the minimum levels possible, and be sure to release it as soon as you can. http://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP – Rohit Oct 17 '11 at 05:18