Android-Integration

Location:

Enable Location and Run-time permissions

To enable location, call the requestPermissions and requestLocationServices method. For Android 6.0 and above, calling this method will trigger a location permission popup that the user has to allow.

Text

                                
                                    
  if(!GeoSpark.checkPermission(context)) {
     GeoSpark.requestPermission(this);
  } else if (!GeoSpark.checkLocationServices(context)) {
     GeoSpark.requestLocationServices(this);
  } else {
     GeoSpark.startTracking(context);
  }
  @Override
  public void onRequestPermissionsResult(int requestCode, String
  permissions[], int[] grantResults) {
     switch (requestCode) {
      case GeoSpark.REQUEST_CODE_PERMISSION:
        if (grantResults.length > 0
          && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          if (ContextCompat.checkSelfPermission(context,

  android.Manifest.permission.ACCESS_FINE_LOCATION)
          == PackageManager.PERMISSION_GRANTED) {
          }
        }
      break;
     }
  }  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent
  data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == GeoSpark.REQUEST_CODE_LOCATION_ENABLED) {
     }
  }          
                    
                               
                            

To start tracking the location, use the startTracking() method. You can keep SDK to track location, or turn it off if you want to stop tracking the user at any point of time using the stopTracking() method.

Text

                                
                                    
  GeoSpark.startTracking(this); 

                              
                            

Stop Location Tracking

You can stop tracking the user at any point of time using the stopTracking() method.

Text

                                
                                    
  GeoSpark.stopTracking(this);    
                            
                               
                            

Location listener

To listen for location in the background, create a class that extends GeoSparkReceiver. Then, register the receiver by adding a receiver element to the application element in your manifest. If above nougat version to get location in background, create the class MyLocationService extends JobService. Then add service to the application element in your manifest.

Your receiver should implement the following:

Text

                                
                                    
    public class MyGeoSparkReceiver extends GeoSparkReceiver {
         @Override
         public void onLocationUpdated(Context context, Location location,
    GeoSparkUser geoSparkUser, String activity){
                  // do something with context, location, user, Activity
    recognition
         }
    }
                           
                               
                            

Text

                                
                                    
    public static void locationJob(Context context) {
         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
         JobScheduler jobScheduler = (JobScheduler)
    context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
         JobInfo job = new JobInfo.Builder(1001, new ComponentName(context,
    MyLocationService.class))
             .setMinimumLatency(1000)
             .setOverrideDeadline(2000)
             .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
             .setPersisted(true)
             .build();
         jobScheduler.schedule(job);
         }
    }
                           
                               
                            

Text

                                
                                    
    public class MyLocationService extends JobService {
         MyGeoSparkReceiver mMyGeoSparkReceiver;

         @Override
         public void onCreate() {
             super.onCreate();
             mMyGeoSparkReceiver = new MyGeoSparkReceiver();
         }
         @Override
         public int onStartCommand(Intent intent, int flags, int startId) {
             return START_NOT_STICKY;
         }
         @Override
         public boolean onStartJob(JobParameters params) {
             try {
             registerReceiver(mMyGeoSparkReceiver, new
    IntentFilter("com.geospark.android.RECEIVED"));
         } catch (Exception e) {
         }
         return true;
     }
         @Override
         public boolean onStopJob(JobParameters params) {
             try {
                unregisterReceiver(mLocationReceiver);
             } catch (Exception e) {
             }
             return true;
         }
     }
                            
                               
                            

Text

                                
                                    
     < application android:label="@string/app_name" >
     ...

    < receiver android:name=".MyGeoSparkReceiver"
                android:enabled="true"
                android:exported="false" >
    < intent-filter >
            < action android:name="com.geospark.android.RECEIVED" / >
    < /intent-filter >
    < /receiver >

    < service
         android:name=".MyLocationService"
         android:exported="false"
         android:permission="android.permission.BIND_JOB_SERVICE" / >

     ...

    < /application >
                           
                               
                            

Trip:

Start Trip

Start trips in Geospark SDK, using startTrip() method.

Text

                                
                                    
  GeoSpark.startTrip(TripActivity.this, "Description", new
  GeoSparkTripCallBack() {
      @Override
      public void onSuccess(GeoSparkTrip geoSparkTrip) {
              geoSparkTrip.getTripId();
              geoSparkTrip.getTripStartedAt();
      }

      @Override
      public void onFailure(GeoSparkError geoSparkError) {
              geoSparkError.getErrorCode();
              geoSparkError.getErrorMessage();
      }
  });

                               
                            

End Trip

End trips in Geospark SDK, using endTrip() method.

Text

                                
                                    
  GeoSpark.endTrip(this, "tripId", new GeoSparkTripCallBack() {
     @Override
     public void onSuccess(GeoSparkTrip geoSparkTrip) {
                 geoSparkTrip.getTripId();
                 geoSparkTrip.getTripEndedAt();
     }
     @Override
     public void onFailure(GeoSparkError geoSparkError) {
                 geoSparkError.getErrorCode();
                 geoSparkError.getErrorMessage();
     }
  });                                
                               
                            

Active Trips

Get active trips in GeoSpark SDK, using activeTrips() method.

Text

                                
                                    
  GeoSpark.activeTrips(this, new GeoSparkTripsCallBack() {
     @Override
     public void onSuccess(GeoSparkTrips geoSparkTrips) {
                 //List of GeoSparkTrips
                 geoSparkTrips.getTripsId();
     }
     @Override
     public void onFailure(GeoSparkError geoSparkError) {
                 geoSparkError.getErrorCode();
                 geoSparkError.getErrorMessage();
     }
  });     
                           
                               
                            

Geofence:

Create Geofence

Create Geofence in GeoSpark SDK, using createGeofence() method.

Text

                                
                                    
GeoSpark.createGeofence(this, latitude, longitude, radius, expireInSeconds, new GeoSparkGeofenceCallBack() { @Override public void onSuccess(GeoSparkGeofence geoSparkGeofence) { geoSparkGeofence.getId(); geoSparkGeofence.getCreatedAt(); geoSparkGeofence.getExpiresAt(); geoSparkGeofence.getCoordinates(); geoSparkGeofence.getRadius(); } @Override public void onFailure(GeoSparkError geoSparkError) { geoSparkError.getErrorCode(); geoSparkError.getErrorMessage(); } });

Geofence List

Get created geofences in GeoSpark SDK, using geofenceList() method.

Text

                                
                                    
  GeoSpark.geofenceList(this, new GeoSparkGeofenceCallBack() {
     @Override
     public void onSuccess(GeoSparkGeofence geoSparkGeofence) {
                  geoSparkGeofence.getGeofenceList();
     }
     @Override
     public void onFailure(GeoSparkError geoSparkError) {
                   geoSparkError.getErrorCode();
                   geoSparkError.getErrorMessage();
     }
  });  
                              
                               
                            

Delete Geofence

Delete Geofence in GeoSpark SDK, using deleteGeofence() method.

Text

                                
                                    
  GeoSpark.deleteGeofence(this, "GeofenceId", new GeoSparkGeofenceCallBack()
  {
      @Override
      public void onSuccess(GeoSparkGeofence geoSparkGeofence) {
          geoSparkGeofence.getId();
          geoSparkGeofence.getMessage();
      }
      @Override
      public void onFailure(GeoSparkError geoSparkError) {
          geoSparkError.getErrorCode();
          geoSparkError.getErrorMessage());
      }
  });  
                              
                               
                            

User:

Update user Description

This will help to add a description to the user so that it is easier for you to identify your users, while tracking them in SDK. And our SDKs provide you with the option to modify the description any time for a user. This can help you easily identify users on dashboard simply via the description.