2010-01-02 1 views
20

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(); 
    } 
} 
+0

NULL을 기억하십시오. - 모든 인 텐트에 '위치'객체가 없습니다. –

답변

20

OK, 나는에 방송 수신기 코드에서 KEY_LOCATION_CHANGED을 변경하여 그것을 해결하기 위해 관리 및 위치 개체를 들고있는 의도. 귀하가 제공 한 정보에 따르면, 귀하는 BroadcastReceiver 측에서 null 객체 검색을 극복 할 수있었습니다. 당신이 관찰하지 못했던 것은 이제 의도가 처음 생성 된 것과 동일한 위치를 받아야한다는 것입니다 (또한 의도 된 캐싱 문제로 보았습니다).

이 문제를 극복하기 위해 많은 사람들이 제안한 FLAG_CANCEL_CURRENT를 사용했으며 신선한 (수분이 많은 : P) 위치 값을 가져 와서 꽤 잘 작동합니다. 그래서 보류중인 의도를 정의하는 행은 다음과 같아야합니다

  • 당신의 목적은 위치 값을받을 만했다
  • 한 번 당신은 극복하기 위해 관리 :

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 
        0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    

    를하지만, 경우에 당신이 무시할 수 다른 방법으로 게시물에 표시되지 않습니다.

14

및 테스트 :

+0

사람, 내 날을 저장했습니다. :디 – Davita

관련 문제