Google location API: request location updates with pending intent?
Asked Answered
P

1

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.

Prioress answered 24/7, 2013 at 6:53 Comment(0)
T
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
    }
Tegantegmen answered 24/7, 2013 at 7:17 Comment(8)
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?Prioress
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 :)Tegantegmen
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 =/Prioress
i've already answered here SO it make sures that cpu is running..Tegantegmen
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.Prioress
@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 :)Tegantegmen
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 :)Prioress
@Sigfrid Johansson :)Tegantegmen

© 2022 - 2024 — McMap. All rights reserved.