9

I have started to implement the Google Location API using this tutorial.

I've managed to get it to work in my application quite fine, it updates my location at the right intervals etc. Now I am working on how to update my location when the device is in sleep mode. According to the documentation, this method is the way to go:

public void requestLocationUpdates (LocationRequest request, PendingIntent callbackIntent);

My question is, how do I set up this PendingIntent, and how do I handle it? I've seen tutorials of how to handle other types of intent, but I am not sure how to apply them to this.

HigiPha
  • 742
  • 9
  • 19

1 Answers1

4

You can either register Broardcast Reciever or Activity through pending intent.Sample Example for registering boardcast reciever:

    String proximitys = "ACTION";
    IntentFilter filter = new IntentFilter(proximitys);
    registerReceiver(mybroadcast, filter);
    Intent intent = new Intent(proximitys);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.requestLocationUpdates(provider, mintime, mindistance,
            proximityIntent);

Your Broardcast Reciever:

public class ProximityIntentReceiver extends BroadcastReceiver {

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context arg0, Intent intent) {
           //action to be performed
    }
Srikanth Roopa
  • 1,782
  • 2
  • 13
  • 19
  • Thank you for your quick answer! However, I have some questions about the code. What exactly is "mybroadcast", and where do you declare it? And I am not using a LocationManager but a LocationClient, will this code work anyways? – HigiPha Jul 24 '13 at 08:58
  • sorry i forgot to mention mybroadcast is the BroadcastReceiver which u want to listen i've never worked on location client but if are able to pass pending intent it should work :) – Srikanth Roopa Jul 24 '13 at 09:42
  • I've tried it out, and it updates the location like the LocationListener: it works fine when the screen is lit, but when i put it in sleep mode, or go to home screen and start up other applications, it won't be updating the location, unfortunately =/ – HigiPha Jul 24 '13 at 11:40
  • i've already answered here [SO](http://stackoverflow.com/questions/17543784/android-can-androdid-alarmmanager-wake-up-the-device-to-perform-the-scheduled-t/17543889#17543889) it make sures that cpu is running.. – Srikanth Roopa Jul 24 '13 at 12:06
  • If I use this approach, wouldn't it be very unnecessary to request updates from the LocationClient with requestLocationUpdates, and instead use getLastLocation? However, it seems that this function does not search for the current location, but gets it's passively. Thank you so much for spending time on my problem :) I just want to fix it in a good way. – HigiPha Jul 24 '13 at 14:12
  • @Sigfrid Johansson can u exactly tell him what u r trying to do so that i can give better solution since the question was about was abt pending intents i answered that :) – Srikanth Roopa Jul 24 '13 at 16:51
  • Oh, yeah, that's right! I think I'll have to make a new question for that in that case. Your answer about the pending intents worked just fine, thank you :) – HigiPha Jul 24 '13 at 21:59
  • @Sigfrid Johansson :) – Srikanth Roopa Jul 25 '13 at 01:22