2017-03-02 16 views
1

終了時にgeoFenceを更新する必要があります。そのために私はその時点で現在の場所が必要です。私のアプリがを閉じているとき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); 
    } 

放送受信機での現在位置を取得する彼らのどのような方法があります。しかしアプリケーションが終了したときに終了時にcurrentLocationを渡す方法が見つかりません。

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

これにインテントサービスを使用していますか? – mechdon

+0

はい私はそれが放送受信機であるべきであると私は知っている – Nepster

+0

あなたの実際のジオフェンスの要求はどこですか? –

答えて

0

あなたの放送でLocationListenerインターフェイスを実装することができますし、オーバーライドメソッドは、一度、彼らが破壊される可能性があるため、私は、IntentServiceまたはBroadcastReceiver内LocationListenerを使ってお勧めしません。この

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

ようonLocationChanged使用します関数(onHandleIntentまたはonReceive)が終了しました。 geofenceイベントを取得すると、LocationManagerでPendingIntentを使用して単一の更新をリクエストします。 githubのPendingIntent(https://github.com/pablobaxter/AndroidLocationExamples)を使って位置更新をリクエストする方法をデモンストレーションするサンプルアプリケーションがあります。

編集:

FYI、IntentServiceは間違っていません。メインスレッドで実行中のonReceiveのバックグラウンドスレッドでonHandleIntentロジックを実行しているだけです。

関連する問題