Android의 LocationManager requestLocationUpdates를 사용하려고합니다. 내 브로드 캐스트 수신기에서 실제 위치 개체를 추출하려고 시도 할 때까지 모든 것이 작동합니다. Android LocationManager가 requestLocationUpdates에 전달하기 전에 사용자 정의 인 텐트에 "추가 기능"을 구체적으로 정의해야합니까?이를 의도에 추가하는 방법을 알고 있거나 해고를 통과 할 때 추가 번들을 생성합니다. 방송 수신기 의도?Android : LocationManager.requestLocationUpdates()를 사용할 때 인 텐트 번들 엑스트라에서 위치 정보를 얻는 방법
내 코드는 다음과 같습니다
<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();
}
}
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);
내가 같은 선언에 정의 된 방송 수신기가
내 "loc"개체가 null로 올 것입니다. 내가 근접 경고와 관련된 유사한 문제에 직면하고 있기 때문에, 당신은 제안 된 솔루션을 내가 코드를 해봤
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();
}
}
NULL을 기억하십시오. - 모든 인 텐트에 '위치'객체가 없습니다. –