2016-06-25 3 views
1

私のメインアクティビティにBroadcastReceiverがあり、これはGoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,ResultCallback<Status>を実装しています。しかし、私は私がでthisを使用することはできませんエラーが発生しますBroadcastReceiverでGeofencingApiと.setResultCallback()を使用する

LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(),getGeofencePendingIntent()).setResultCallback(this); 

は今、私は経由で私のジオフェンスを追加します。 setResultCallback、これはおそらくBroadcastreceiverで呼ばれていることと関係があります。 結果がonResult()で処理されているため、onResult()をどのように取得するのですか? setResultCallback

マイbroadcastreceiver:アクティビティのonCreateで

WakefulBroadcastReceiver mMessageReceiver = new WakefulBroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      SharedPreferences mahprefs = PreferenceManager.getDefaultSharedPreferences(getApplication()); 
      Integer radius = mahprefs.getInt("radiusnumber",500); 
      String lat = (String) intent.getExtras().get("Latitude"); 
      String longi = (String) intent.getExtras().get("Longitude"); 
      Double alt = intent.getDoubleExtra("Altitude", 0); 
      Boolean geofenceexists = mahprefs.getBoolean("geofenceexists",false); 
      Boolean scharf = mahprefs.getBoolean("scharf",false); 
      Float accuracys = intent.getFloatExtra("Accuracy", 0); 
      String providertype = intent.getStringExtra("Provider"); 
      Number speed = intent.getFloatExtra("Speed", 0); 
      latText.setText(lat); 
      longText.setText(longi); 
      altitude.setText(String.valueOf(alt)+getString(R.string.meter)); 
      accuracy.setText(String.valueOf(accuracys)+getString(R.string.meter)); 
      lcprovidertype.setText(String.valueOf(providertype)); 
      Float bearing = intent.getFloatExtra("Bearing", 0); 
      Log.v("INTENTgetextra", lat); 
      Log.v("INTENTgetextra", longi); 
      SharedPreferences.Editor editor = mahprefs.edit(); 
      editor.putString("latitude", lat); 
      editor.putString("longitude", longi); 
      editor.putString("altitude", String.valueOf(alt)); 
      editor.putString("speed", String.valueOf(speed)); 
      editor.putString("bearing", String.valueOf(bearing)); 
      editor.apply(); 

      //Geofence Managment 
      if(scharf){ 
       if(!geofenceexists){ 
        mGeofenceList.add(new Geofence.Builder().setRequestId("alarmgeofence").setCircularRegion(Double.parseDouble(lat), Double.parseDouble(longi), radius) 
          .setExpirationDuration(Geofence.NEVER_EXPIRE) 
          .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
          .build()); 
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(),getGeofencePendingIntent()).setResultCallback(this); 
        Log.v("GEOFENCES",mGeofenceList.toString()); 
        editor.putBoolean("geofenceexists",true); 
        editor.apply(); 
       } 
      } else{ 
       try { 
        mGeofenceList.removeAll(mGeofenceList); 
        LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, getGeofencePendingIntent()).setResultCallback(onResult(Status null); // Result processed in onResult(). 
        editor.putBoolean("geofenceexists",false); 
        editor.apply(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 




     } 
    }; 

mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

My活動:public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,ResultCallback<Status>(そして、はい、必要なすべてのメソッドがインポートされている)

そして、私の輸入:

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.ResultCallback; 
import com.google.android.gms.common.api.Status; 
import com.google.android.gms.location.Geofence; 
import com.google.android.gms.location.GeofencingRequest; 
import com.google.android.gms.location.LocationServices; 
+0

あなたのインポートと '.setResultCallback(this); 'の呼び出しを含む関数を共有できますか? – antonio

+0

@antonio確かに、私の編集を見てください。 –

答えて

1

私はあなたのMainActivityResultCallbackを実装し、ので、あなたのコードは次のようになりますことを理解:

LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(),getGeofencePendingIntent()).setResultCallback(MainActivity.this); 
+0

それはそうだ。ありがとう!私はできるだけ早くあなたの答えを受け入れます。 –

0

これは非常にシンプルなもののために仕事のとんでもない量です。 iOSでさえ、ジオフェンシングにもっときれいに取り組んでいます。

関連する問題