【问题标题】:Using GeofencingApi and .setResultCallback() in BroadcastReceiver在 BroadcastReceiver 中使用 GeofencingApi 和 .setResultCallback()
【发布时间】:2016-06-25 16:27:36
【问题描述】:

假设我的 Main Activity 中有一个 BroadcastReceiver,它实现了GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,ResultCallback<Status>

现在,我想通过以下方式添加我的地理围栏:

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

但是,我收到一个错误,我无法在 .setResultCallback 中使用 this,这可能与在广播接收器中调用它有关。 由于 Result 正在 onResult() 中处理,我如何在 .setResultCallback 中获取 onResult()

我的广播接收者:

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




        }
    };

在活动 onCreate:

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

我的活动: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;

【问题讨论】:

  • 你能分享你的导入和包含对.setResultCallback(this);的调用的函数吗?
  • @antonio 当然,看看我的编辑。

标签: java android location android-geofence location-services


【解决方案1】:

我了解您的MainActivity 实现了ResultCallback,因此您的代码应如下所示:

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

【讨论】:

  • 是的,似乎就是这样。谢谢!我会尽快接受你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多