Google map and passive provider

耗尽温柔 提交于 2019-12-12 03:16:17

问题


I'm working with passive provider, with this code.

Intent intent = new Intent(PASSIVE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, pi);

and I define broadcast receiver in AndroidManifest.xml

<receiver
    android:name="passiveLocationReceiver"
    android:enabled="true" >
        <intent-filter>
            <action android:name="com.passive_location_update" />
        </intent-filter>
</receiver>

passiveLocationReceiver is

public class passiveLocationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!PASSIVE_ACTION.equals(action))
            return;

        Log.d("chitacan","Passive Provider Event!!!");
        }
    }

This code works fine when other geo location applications(facebook, foursquare.. etc) But with "Google Map" application, It doesn't.(d.android.com explains "PASSIVE_PROVIDER" that can be updated when other application requests locations.)

Even though I can see my location with "Google Map" application, but I can't get any event from passive provider.(I can't see passiveLocationReceiver's log message.)

Well, I tried to dump LocationManager's state in command line,

$adb shell dumpsys location

However LocationManager's state didn't change anything. Seems like "Google Map" Application doesn't use LocationManager.

Is there any way to get passive event when "Google Map" application updates my location?? or Am I doing something wrong??


回答1:


requestLocationUpdates will only receive changes or "updates" to location values. Google Maps will broadcast changes in location values that are provided by the GPS Location provider.

If testing from an Emulator, try entering a new value for your mock location. Otherwise, test while walking around.

However, I have noticed too that Wi-Fi (network) locations changes are not broadcast from Google Maps like they are from other similar apps.

In your case- perhaps the GPS is not enabled, or it hasn't changed location from its current state and therefore you are not receiving any "updates" in location to this listener. (I found this topic because I was wondering why Google Maps was not broadcasting Wi-Fi location changes if anyone figures that out)




回答2:


Recent Google Map and other map services utilize new version of location APIs based on Google Play Services. They don't merely rely on only GPS or Network Provider, but they use "Fused" one. For this reason, legacy PASSIVE_PROVIDER can't catch location updates with "fused" method.

You'd better to use new APIs with LocationClient and LocationRequest with PRIORITY_NO_POWER. check following codes

        LocationRequest lRequest = LocationRequest.create()
                                              .setPriority(LocationRequest.PRIORITY_NO_POWER)
                                              .setInterval(mMIN_LOCATION_UPDATE_INTERVERAL_IN_MILLISECONDS);

    Intent locationIntent = new Intent(mPASSIVE_LOCATION_INTENT_ACTION_STRING);
    mPassiveLocationPendingIntent = PendingIntent.getBroadcast(mContext, 0, locationIntent ,PendingIntent.FLAG_UPDATE_CURRENT);
    mPassiveLocationClient.requestLocationUpdates(lRequest, mPassiveLocationPendingIntent);


来源:https://stackoverflow.com/questions/9620107/google-map-and-passive-provider

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