Android的:如何使用LocationManager.requestLocationUpdates时,得到的意图捆绑额外的位置信息() [英] Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()

查看:1195
本文介绍了Android的:如何使用LocationManager.requestLocationUpdates时,得到的意图捆绑额外的位置信息()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Android的LocationManager requestLocationUpdates。一切工作,直到我尝试提取的实际位置的对象,在我的广播接收机。我是否需要专门定义的额外我的自定义的意图,使得Android的LocationManager之前,我把它传递给requestLocationUpdates所以它知道如何将它添加到意图,或不管将它创建额外的束时,它通过发射意图广播接收机?

I am trying to use Android's LocationManager requestLocationUpdates. Everything is working until I try to extract the actual location object that in my broadcast receiver. Do I need to specifically define the "extras" to my custom intent so that the Android LocationManager before I pass it to requestLocationUpdates so it knows how to add it into the intent, or will it create the extras-bundle regardless when it passes the fired intent to the broadcast receiver?

我的code是这样的:

My code looks like this:

Intent intent = new Intent("com.myapp.swarm.LOCATION_READY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
    0, intent, 0);

//Register for broadcast intents
int minTime = 5000;
int minDistance = 0;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
    minDistance, pendingIntent);

我有一个在宣言中定义为一个广播接收器:

I have a broadcast receiver that is defined in the manifesto as:

<receiver android:name=".LocationReceiver">
    <intent-filter>
        <action android:name="com.myapp.swarm.LOCATION_READY" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

和广播接收器类:

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //Do this when the system sends the intent
    Bundle b = intent.getExtras();
    Location loc = (Location)b.get("KEY_LOCATION_CHANGED");

    Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); 
    }
}

我的禄的对象快到了空。

My "loc" object is coming up null.

推荐答案

OK,我设法通过改变KEY_LOCATION_CHANGED在广播接收器code,以解决它:

OK, I managed to fix it by changing the KEY_LOCATION_CHANGED in the broadcast receiver code to:

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //Do this when the system sends the intent
    Bundle b = intent.getExtras();
    Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);

    Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); 
    }
}

这篇关于Android的:如何使用LocationManager.requestLocationUpdates时,得到的意图捆绑额外的位置信息()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆