com.google.android.gms.location.GeofencingClient Java Examples

The following examples show how to use com.google.android.gms.location.GeofencingClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        GeofencingClient geofencingClient = LocationServices.getGeofencingClient(this);
        geofencingClient.addGeofences(createGeofencingRequest(), createGeofencePendingIntent())
                .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(MainActivity.this, "onSuccess()", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this,
                                "onFailure(): " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    } else {
        ActivityCompat.requestPermissions(this,
                new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION},1);
    }
}
 
Example #2
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 6 votes vote down vote up
@Override
protected void onStop() {
    super.onStop();

    GeofencingClient geofencingClient = LocationServices.getGeofencingClient(this);
    geofencingClient.removeGeofences(createGeofencePendingIntent())
            .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    //Success
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    //Failure
                }
            });
}
 
Example #3
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
private void listing15_14_15_16_17(Location location, String id) {
  if (
    ActivityCompat
      .checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
    ActivityCompat
      .checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {

      Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
      PendingIntent geofenceIntent = PendingIntent.getBroadcast(this, -1, intent, 0);

      // Listing 15-14: Accessing the Geofencing Client
      GeofencingClient geofencingClient =
        LocationServices.getGeofencingClient(this);

      // Listing 15-15: Defining a Geofence
      Geofence newGeofence
        = new Geofence.Builder()
            .setRequestId(id) // unique name of geofence
            .setCircularRegion(location.getLatitude(),
              location.getLongitude(),
              30) // 30 meter radius.
            .setExpirationDuration(Geofence.NEVER_EXPIRE) // Or expiration time in ms
            .setLoiteringDelay(10 * 1000)                   // Dwell after 10 seconds
            .setNotificationResponsiveness(10 * 1000)       // Notify within 10 seconds
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL)
            .build();

      // Listing 15-16: Creating a Geofencing Request
      GeofencingRequest geofencingRequest
        = new GeofencingRequest.Builder()
            .addGeofence(newGeofence)
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
            .build();

      // Listing 15-17: Initiating a Geofencing Request
      geofencingClient.addGeofences(geofencingRequest, geofenceIntent)
        .addOnSuccessListener(this, new OnSuccessListener<Void>() {
          @Override
          public void onSuccess(Void aVoid) {
            // TODO Geofence added.
          }
        })
        .addOnFailureListener(this, new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "Adding Geofence failed", e);
            // TODO Geofence failed to add.
          }
        });
    }
}