Android の LocationManager requestLocationUpdates を使用しようとしています。ブロードキャストレシーバーにある実際の位置オブジェクトを抽出しようとするまで、すべてが機能しています。Android LocationManager を requestLocationUpdates に渡す前にカスタム インテントに「エクストラ」を具体的に定義して、それをインテントに追加する方法を認識させる必要がありますか。放送受信機への意図?
私のコードは次のようになります。
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);
マニフェストで次のように定義されている放送受信機があります。
<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();
}
}
「loc」オブジェクトが null になります。