我如何删除地理围栏?每次我点击重置按钮,它都会显示以下错误 -
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
这是我的代码 -
public class GeofenceStore implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status>, LocationListener {
private Context mContext;
/**
* Google API client object.
*/
private GoogleApiClient mGoogleApiClient;
/**
* Geofencing PendingIntent
*/
private PendingIntent mPendingIntent;
/**
* List of geofences to monitor.
*/
private ArrayList<Geofence> mGeofences;
/**
* Geofence request.
*/
private GeofencingRequest mGeofencingRequest;
/**
* Location Request object.
*/
private LocationRequest mLocationRequest;
public GeofenceStore(Context context, ArrayList<Geofence> geofences) {
mContext = context;
mGeofences = new ArrayList<Geofence>(geofences);
mPendingIntent = null;
// Build a new GoogleApiClient, specify that we want to use LocationServices
// by adding the API to the client, specify the connection callbacks are in
// this class as well as the OnConnectionFailed method.
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
Log.i("APi is :", "+" + mGoogleApiClient);
mLocationRequest = new LocationRequest();
// We want a location update every 10 seconds.
mLocationRequest.setInterval(10000);
// We want the location to be as accurate as possible.
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
// We're connected, now we need to create a GeofencingRequest with
// the geofences we have stored.
mGeofencingRequest = new GeofencingRequest.Builder().addGeofences(
mGeofences).build();
mPendingIntent = createRequestPendingIntent();
// This is for debugging only and does not affect
// geofencing.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
GeofenceStore.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
// Submitting the request to monitor geofences.
PendingResult<Status> pendingResult = LocationServices.GeofencingApi
.addGeofences(mGoogleApiClient, mGeofencingRequest,
mPendingIntent);
// Set the result callbacks listener to this class.
pendingResult.setResultCallback(this);
}
private void requestPermissions(String[] strings, int i) {
switch(i)
{
case 100: {
{
Log.i("thanks","asdm");
}
}
}
}
public void removeGeofence(){
LocationServices.GeofencingApi.removeGeofences(
mGoogleApiClient,
// This is the same pending intent that was used in addGeofences().
mPendingIntent
).setResultCallback(this); // Result processed in onResult().
}
private void requestPermissions(int requestCode,String[] permissions, int[] grantResults) {
switch (requestCode) {
case 100: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(mContext, "Thanks for the permission", Toast.LENGTH_LONG).show();
// permission was granted, yay! do the
// calendar task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(mContext, "You did not allow to access your current location", Toast.LENGTH_LONG).show();
}
}
// other 'switch' lines to check for other
// permissions this app might request
}
}
/**
* This creates a PendingIntent that is to be fired when geofence transitions
* take place. In this instance, we are using an IntentService to handle the
* transitions.
*
* @return A PendingIntent that will handle geofence transitions.
*/
private PendingIntent createRequestPendingIntent() {
if (mPendingIntent == null) {
Log.v("HERE", "Creating PendingIntent");
Intent intent = new Intent(mContext, GeofenceIntentService.class);
Log.i("another class called",""+intent);
mPendingIntent = PendingIntent.getService(mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try {
mPendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
return mPendingIntent;
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v("THE CONNECTION HAS", "failed.");
}
public void onSuccess(AsyncTask.Status status) {
}
public void onFailure(Status status) {
}
@Override
public void onResult(Status result) {
if (result.isSuccess()) {
Log.v("THE RESULT IS", "Success!");
} else if (result.hasResolution()) {
// TODO Handle resolution
} else if (result.isCanceled()) {
Log.v("THE RESULT IS", "Canceled");
} else if (result.isInterrupted()) {
Log.v("THE RESULT IS", "Interrupted");
} else {
}
}
}
这里是从我的地图活动中调用的函数,我使用了一个重置按钮来重置添加的地理围栏.Pendind intent可以在上面提到的代码中找到。我想知道它有什么问题? 按钮是 -
public void Reset(View view){
db.execSQL("DELETE FROM Coordinates");
//delete all rows in a table
request=request-1;
GeofenceStore mgeofencestore=new GeofenceStore(this,mGeofences);
mgeofencestore.removeGeofence();
db.close();
Toast.makeText(this,"The Data was reset,Please click on 'Add Geofence' to add more Geofence",Toast.LENGTH_LONG).show();
}
答案 0 :(得分:1)
每次创建新的GeofenceStore对象时,您都会创建一个新的GoogleAPIClient。由于您之后立即致电removeGeofences()
,因此GoogleAPIClient永远不会有机会连接,并抛出您所看到的错误。要么放置最初在调用类中创建的GeofenceStore对象的副本,要么创建static instance method以获取当前对象并避免重新创建它。