【问题标题】:Google map and passive provider谷歌地图和被动提供者
【发布时间】:2012-03-08 15:24:24
【问题描述】:

我正在与被动提供商合作,使用此代码。

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);

我在 AndroidManifest.xml 中定义广播接收器

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

passiveLocationReceiver 是

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!!!");
        }
    }

此代码在其他地理位置应用程序(facebook、foursquare.. 等)时可以正常工作 但是对于“谷歌地图”应用程序,它不会。(d.android.com 解释了当其他应用程序请求位置时可以更新的“PASSIVE_PROVIDER”。)

虽然我可以通过“谷歌地图”应用程序看到我的位置,但我无法从被动提供者那里获得任何事件。(我看不到passiveLocationReceiver 的日志消息。)

好吧,我尝试在命令行中转储 LocationManager 的状态,

$adb shell dumpsys location

但是 LocationManager 的状态并没有改变任何东西。看起来像“谷歌地图”应用程序 不使用 LocationManager。

当“谷歌地图”应用程序更新我的位置时,有没有办法获得被动事件? 还是我做错了什么??

【问题讨论】:

    标签: android location locationmanager


    【解决方案1】:

    最近的 Google 地图和其他地图服务使用基于 Google Play 服务的新版位置 API。他们不仅依靠 GPS 或网络提供商,而且还使用“融合”之一。出于这个原因,旧的 PASSIVE_PROVIDER 无法使用“融合”方法捕获位置更新。

    您最好将新的 API 与 LocationClient 和 LocationRequest 与 PRIORITY_NO_POWER 一起使用。检查以下代码

            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);
    

    【讨论】:

    • ...但是您无法判断该位置是来自 GPS 还是网络或带有 Fused Location 的 WiFi。
    【解决方案2】:

    requestLocationUpdates 只会接收位置值的更改或“更新”。 Google 地图将广播 GPS 位置提供商提供的位置值的变化。

    如果从模拟器进行测试,请尝试为您的模拟位置输入一个新值。否则,请边走边测试。

    但是,我也注意到,Wi-Fi(网络)位置更改不会像从其他类似应用程序那样从 Google 地图广播。

    在您的情况下 - 也许 GPS 未启用,或者它没有从当前状态更改位置,因此您没有收到任何“更新”位置到此侦听器。 (我发现这个话题是因为我想知道为什么如果有人发现 Google 地图不广播 Wi-Fi 位置变化)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多