Google location API: request location updates with pending intent?

时光毁灭记忆、已成空白 提交于 2019-12-06 22:49:54

问题


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.


回答1:


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
    }


来源:https://stackoverflow.com/questions/17827121/google-location-api-request-location-updates-with-pending-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!