Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GPS tracking and WakeLock

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:

  • how to keep application running in background? keep collecting data? (pointed me to MyTracks)
  • Tracking user's sms and geo position in the background - is my idea working?
  • Android service with always on GPS
like image 623
Ralf Avatar asked Oct 14 '11 08:10

Ralf


2 Answers

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.

like image 61
Vineet Shukla Avatar answered Oct 04 '22 06:10

Vineet Shukla


  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....!!

like image 28
Rohit Avatar answered Oct 04 '22 05:10

Rohit