2017-11-07 12 views
0

AndroidでGeofencingApiの代わりにGoogle GeofencingClientを使用しようとしています。ジオフェンスを設定して追加した後、私は成功リスナーからコールバックを受け取り、IntentServiceの最初のジオフェンス入力/終了イベントを受け取ります。しかし、私がその地域を出入りする際に、その後のジオフェンスイベントは発生しません。半径を200メートルに設定しました。ここで私はジオフェンスを追加するために使用しているコードです:geofencingEvent.hasError()がtrueを返したときに私のIntentServiceでAndroid Google GeofencingClientはその後のイベントを送信しません

Geofence.Builder geoBuilder = new Geofence.Builder(); 
    geoBuilder.setRequestId(Constants.GEOFENCE_ID); 
    geoBuilder.setNotificationResponsiveness(5 * 60 * 1000); 
    geoBuilder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT); 
    geoBuilder.setCircularRegion(latitude, longitude, 200f); 
    geoBuilder.setExpirationDuration(Geofence.NEVER_EXPIRE); 

    GeofencingRequest.Builder reqBuilder = new GeofencingRequest.Builder(); 
    reqBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT); 
    reqBuilder.addGeofence(geoBuilder.build()); 

    PendingIntent pi = PendingIntent.getService(context, 0, 
      new Intent(context, GService.class), PendingIntent.FLAG_UPDATE_CURRENT); 

    GeofencingClient client = LocationServices.getGeofencingClient(context); 
    Task task = client.addGeofences(reqBuilder.build(), pi); 
    task.addOnSuccessListener(new OnSuccessListener() { 
     @Override 
     public void onSuccess(Object o) { 
      Toast.makeText(context, "SUCCESS", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    task.addOnFailureListener(new OnFailureListener() { 
     @Override 
     public void onFailure(@NonNull Exception e) { 
      Toast.makeText(context, "FAIL", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

が、私はエントリをログに記録します。しかし、私はエラーログエントリを見ていないので、最初のイベントの後にジオフェンシングイベントは全くサービスにポストされていません。

助けていただけたら幸いです!

答えて

0

LocationServices.GeofencingApi.addGeofencesからgeofencingClient.addGeofencesに切り替わった後、私は逆の問題を抱えています。現在の場所の周りにジオフェンスを配置すると、最初の「入力」インテントが発生しなくなりました。

前(印刷 "成功" と意図を発射しました):後

 LocationServices.GeofencingApi.addGeofences(
       googleApiClient, 
       request, 
       createGeofencePendingIntent() 
     ).setResultCallback(status -> { 
      Log.i(TAG, "onResult: " + status); 
      if (status.isSuccess()) { 
       mapsActivity.message("Adding geo-fence... Success"); 
       drawGeofence(position, radius); 
      } else { 
       mapsActivity.message("Adding geo-fence... FAILED!"); 
      } 
     }); 

(印刷 "成功" を行いますが、最初の "入力" 発生しません):

 geofencingClient.addGeofences(request, createGeofencePendingIntent()) 
       .addOnSuccessListener((Void)->{ 
        mapsActivity.message("Adding geo-fence... Success"); 
        drawGeofence(position, radius); 
       }).addOnFailureListener((Void)->{ 
        // TODO: inform about fail 
        mapsActivity.message("Adding geo-fence... FAILED!"); 
       }) 
     ; 

更新:これを書いた後、ちょうど(コード変更なしで)作業を開始しました。唯一の違いは、アプリ "Google"がバージョン7.18.50.21.arm64に更新されたことです。

+0

ジオフェンスを追加できなかったことにお気づきになりましたか?私はそれがジオフェンスを追加していないのを見ましたが、なぜそれが第2ジオフェンスのように失敗するのか分かりません。私が知る限り、Googleは説明をしません。 –

関連する問題