java - 在 BroadcastReceiver 中使用 GeofencingApi 和 .setResultCallback()

标签 java android location android-geofence location-services

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

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

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

但是,我收到无法使用的错误 this在 。 setResultCallback ,这可能与它在 Broadcastreceiver 中被调用有关。 由于结果正在 onResult() 中处理, 我如何获得 onResult()在 。 setResultCallback

我的广播接收器:

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




        }
    };

在创建 Activity 中:

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

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

最佳答案

我知道你的 MainActivity 实现了 ResultCallback,所以你的代码应该是这样的:

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

关于java - 在 BroadcastReceiver 中使用 GeofencingApi 和 .setResultCallback(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030533/

相关文章:

Android:如何用最原始的手机方式获取用户的当前位置NAME? (没有互联网或全局定位系统)

android - 删除一个位置提供商 - android

java - If 语句不适用于两个 <Long> 列表 [Java]

java - SSL 没有对等证书 Android

java - 增加代号一中的字体大小?

java - Android 服务在 OPPO 小米 MIUI vivio 等某些设备上停止工作

android - 仅显示第一个 fragment

android - 使用 ViewModel 更改 Fragment 时在 ImageView 中保留位图

google-maps - 如何在 Google Maps Flutter 中将动态位置或用户位置设置为圆圈并显示仅在圆圈内的标记

java - 在 tomcat7 中找不到 Websocket 端点路径