使用挂起的intent获取位置更新会返回null位置

时间:2014-10-28 18:58:45

标签: java android gps android-location

我试图通过将PendingIntent传递给locationClient来检索位置,但我在广播接收器中收到的意图中没有位置。

这是我的代码:

启动LocationRequests

@Override
public void onConnected(Bundle bundle) {
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();

    notifyIntent = new Intent(RunActivity.this, RunActivity.AlarmReciever.class);
    notifySender = PendingIntent
            .getBroadcast(RunActivity.this, 899899, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notifyIntent.setAction("com.blabla.blabla.NOTIFY_GPS");

    locationClient.requestLocationUpdates(locationRequest, notifySender);
}

Reciever

    <receiver android:name=".RunActivity$AlarmReciever"
              android:exported="true">
        <intent-filter>
            <action android:name="com.blabla.blabla.NOTIFY_GPS" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

广播接收器

public static class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RECIEVED LOCATION UPDATE", "INTENT");
        Bundle b = intent.getExtras();
        Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
        Log.d("LOCATION", null == loc ? "emptyloc" : loc.toString());
    }
}

这将始终打印“LOCATION:emptyloc”。

有什么想法吗?

P.S。:忘了提到位置工作正常,如果我打电话locationClient.getLastLocation()我会按预期检索位置。只有PendingIntent才能使该位置为空;

最诚挚的问候。

1 个答案:

答案 0 :(得分:1)

我的不好,位置不是空的,我的钥匙错了。

我通过获取所有捆绑密钥并迭代它们解决了这个问题:

for (String key : b.keySet()) {
    try {
        Log.d("bundle object", b.get(key).toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}