0

I made an application which is sending GPS coordinates via SMS.

So when you click on the button it will try to update Location but because GPS coordinates update needs 10-15 seconds it will jump to acquire lastknowLocation and send the SMS.

-lastKnowLocation is outdated or 0.0, 0.0.

How to delay the sending of the sms, until it acquires new GPS coords?

Adin Pinjo
  • 35
  • 5

1 Answers1

0

You could use BroadCastReceiver to receive your new gps location: BroadcastReceiver for location. You would use requestLocationUpdates version with PendingIntent parameter, this method allows to receive new coordinates even if your app got killed, from docs:

This method is suited for the background use cases, more specifically for receiving location updates, even when the app has been killed by the system. In order to do so, use a PendingIntent for a started service. For foreground use cases, the LocationListener version of the method is recommended, see requestLocationUpdates(LocationRequest, LocationListener).

Inside your broadcast onReceive I would fire an IntentService that would use wakelock: https://github.com/commonsguy/cwac-wakeful. This is because broadcastreceiver processing should always be as short as possible, its always better to start a service - ie. IntentService, and to prevent android from killing your app and service after processing broadcast (android is allowed to do that - but mostly if your app was killed before firing pendingintent) you use wakelock. Inside your IntentService you can safely send your email. You could save sms informations inside sharedpreferences for your app.

All this complications is becasue user can after clickin button, press home button and your app can actually get killed.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100