Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Geofencing avec Google Places

Geofencing avec Google Places

Présentation du Geofencing sur Android avec Google Places.

7352e4e763e3b54ac7a91f013dc7c709?s=128

Robin Caroff

January 24, 2017
Tweet

More Decks by Robin Caroff

Other Decks in Programming

Transcript

  1. GEOFENCING AVEC GOOGLE PLACES ANDROID NANTES - JANVIER 2017 Par

    @RobinCaroff
  2. LE PROBLÈME

  3. GEOFENCING https://developer.android.com

  4. GEOFENCING https://developers.google.com

  5. GEOFENCING Dépendances Gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.android.gms:play-services:9.4.0' }a
  6. dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' a

    compile 'com.google.android.gms:play-services-location:9.4.0' }a GEOFENCING Dépendances Gradle
  7. GEOFENCING dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1'

    a compile 'com.google.android.gms:play-services-location:9.4.0' compile 'com.google.android.gms:play-services-maps:9.4.0' compile 'com.google.android.gms:play-services-places:9.4.0' }a Dépendances Gradle
  8. PERMISSIONS <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.robincaroff.mygeofencer" > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application> ... </application> </manifest> Manifest
  9. PERMISSIONS <application android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/maps_apikey"/> ... <service

    android:name=".services.GeofenceTransitionsIntentService"/> </application> Manifest
  10. PERMISSIONS <application android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/maps_apikey"/> ... <service

    android:name=".services.GeofenceTransitionsIntentService"/> </application> Manifest pour Place Picker
  11. PERMISSIONS La technique

  12. PERMISSIONS La technique du

  13. PERMISSIONS La technique du SPLASH SCREEN

  14. PERMISSIONS @Override protected void onCreate(Bundle savedInstanceState) { ... checkPermissions(); }a

    private void checkPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } else { goToMainActivity(); }b }c
  15. PERMISSIONS @Override protected void onCreate(Bundle savedInstanceState) { ... checkPermissions(); }a

    private void checkPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } else { goToMainActivity(); }b }c
  16. GOOGLE API CLIENT protected synchronized void buildGoogleApiClient() { mGoogleApiClient =

    new GoogleApiClient .Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .build(); }
  17. GOOGLE API CLIENT @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect();

    } @Override protected void onStop() { super.onStop(); mGoogleApiClient.disconnect(); }
  18. GEOFENCES https://developer.android.com

  19. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  20. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  21. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  22. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  23. GEOFENCING REQUEST private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new

    GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); }
  24. GEOFENCE INTENT private PendingIntent getGeofencePendingIntent() { if (mGeofencePendingIntent != null)

    { return mGeofencePendingIntent; } Intent intent = new Intent(this, GeofenceTransitionsIntentService.class); return PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT); }
  25. START / STOP GEOFENCING LocationServices.GeofencingApi.addGeofences( mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent() ).setResultCallback(this); LocationServices.GeofencingApi.removeGeofences(

    mGoogleApiClient, getGeofencePendingIntent() ).setResultCallback(this);
  26. START / STOP GEOFENCING LocationServices.GeofencingApi.addGeofences( mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent() ).setResultCallback(this); LocationServices.GeofencingApi.removeGeofences(

    mGoogleApiClient, getGeofencePendingIntent() ).setResultCallback(this);
  27. None
  28. NOT BORING AT ALL…

  29. SHOW ME HOW IT WORKS !

  30. TESTS ▸ Real testing ▸ Mock location apps ▸ Emulator

    ▸ Mock location provider
  31. LIMITATIONS

  32. LIMITATIONS 100 geofences par application et par utilisateur (pour un

    device)
  33. BEST PRACTICES ▸ Réduire l’utilisation de la batterie en changeant

    la ‘réactivité’ des notifications ▸ Optimiser les rayons de geofences ▸ Surveiller seulement quand c’est nécessaire ▸ Utiliser les transitions DWELL plutôt que ENTER