I need to do background GPS tracking on an Android phone, and I'm currently planning on having two scenarios:
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:
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.
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.
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....!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With