2016-01-31 1 views
0

Comment puis-je supprimer le Geofence? chaque fois que je clique sur le bouton de remise à zéro, il affiche les informations suivantes ERROR-Supprimer Geofence à l'aide du bouton de réinitialisation

Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet. 

Voici mon code-

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 { 

     } 

    } 
} 

et voici la fonction à appeler de mes activités de cartes, j'ai utilisé un bouton de remise à zéro réinitialiser le geofence ajouté. L'intention d'intention peut être trouvée dans le code mentionné ci-dessus. je veux savoir quel est le problème avec ça? Le bouton est-

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

Répondre

1

Vous créez une nouvelle GoogleApiClient chaque fois de créer un nouvel objet GeofenceStore. Puisque vous appelez removeGeofences() immédiatement après, GoogleAPIClient n'a jamais la possibilité de se connecter et renvoie l'erreur que vous voyez. Placez une copie de l'objet GeofenceStore que vous créez initialement dans la classe appelante ou créez un static instance method pour obtenir l'objet actuel et éviter de le recréer.