2017-03-02 8 views
1

출구에서 geoFence을 업데이트해야합니다. 이를 위해서는 그 시점에서 현재 위치가 필요합니다. 내 응용 프로그램이을 닫을 때지오 펜스의 현재 위치 얻기 .GEOFENCE_TRANSITION_EXIT

나는 onLocationChanged에 대한 리스너가 없습니다. 그리고 geoFence를 업데이 트하려면 내 위치가 필요합니다. 잘 작동

private BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 
      if (bundle != null) { 
       int resultCode = bundle.getInt("done"); 
       if (resultCode == 1) { 

        if(!MainActivity.isGeoFenceAdded){ 
         updateGeoFencesOnCurrentExit();//I need current location here 
        } 
       } 
      } 
     } 
    }; 

모든 것은 MainActivityGeofence.GEOFENCE_TRANSITION_EXIT

public void broadcastUpdateGeoFences() { 
     MainActivity.isGeoFenceAdded = false;//It tells to update geoFence 
     Intent intent = new Intent(Constants.RECEIVER_GEOFENCE); 
     intent.putExtra("done", 1); 
     sendBroadcast(intent); 
    } 

방송 수신기에서 현재 위치를 얻기 위해 자신의 방법이입니다. 하지만 앱을 종료하면 exit시 currentLocation을 전달할 수있는 방법을 찾을 수 없습니다. 그들은 한 번 파괴 할 수 있기 때문에

private void updateGeoFencesOnCurrentExit(Location currentLocation){ 
      locationHandler.updateGeoFences(currentLocation); 
} 
+0

여기에 의도 서비스를 사용하고 있습니까? – mechdon

+0

예 브로드 캐스트 수신기 여야한다는 것을 알고 있습니다. – Nepster

+0

실제 지오 펜스 추가 요청은 어디에 있습니까? –

답변

0

당신은 당신의 방송 및 사용 재정의 방법에서 LocationListener 인터페이스를 구현 할 수는 내가 IntentService 또는 브로드 캐스트 리시버 내부 LocationListener를 사용하는 것이 좋습니다 않을 것이

@Override 
public void onLocationChanged(Location location) { 
    location.getLongitude(); 
    location.getLatitude(); 
} 
0

처럼 onLocationChanged 함수 (onHandleIntent 또는 onReceive)가 종료되었습니다. 지오 펜스 이벤트가 발생하면 LocationManager와 함께 PendingIntent를 사용하여 단일 업데이트를 요청합니다. github (https://github.com/pablobaxter/AndroidLocationExamples)의 PendingIntent를 사용하여 위치 업데이트를 요청하는 방법을 보여줄 수있는 예제 앱이 있습니다.

편집 :

참고로 IntentService가 잘못되었습니다. 주 스레드에서 실행중인 onReceive에 대한 백그라운드 스레드에서 onHandleIntent 논리를 실행 중입니다.