项目作者: martinraj

项目描述 :
Tracking the android mobile and animating marker smoothly in Google Maps. Tracking has been done both in foreground and background. Tested in Android Oreo 8.1, Android 6.0 and Android 5.0
高级语言: Java
项目地址: git://github.com/martinraj/tracking-location-provider-android.git


tracking-location-provider-android

In this demo, I have covered both foreground tracking and background tracking.

Foreground Tracking

Foreground Tracking will work only if app is in foreground. To start foreground tracking, tap on the “START FOREGROUND TRACKING” button. Before requesting for location updates, check location settings of the device as below.

  1. mLocationRequest = new LocationRequest();
  2. mLocationRequest.setInterval(30000);
  3. mLocationRequest.setFastestInterval(15000);
  4. mLocationRequest.setSmallestDisplacement(50); // set this as 100 to 200 if user use app while driving or motor riding
  5. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  6. LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
  7. .addLocationRequest(mLocationRequest);
  8. SettingsClient client = LocationServices.getSettingsClient(this);
  9. Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
  10. task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
  11. @Override
  12. public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
  13. // All location settings are satisfied. The client can initialize
  14. // location requests here.
  15. // ...
  16. bService.setVisibility(View.GONE);
  17. startLocationUpdates();
  18. }
  19. });
  20. task.addOnFailureListener(this, new OnFailureListener() {
  21. @Override
  22. public void onFailure(@NonNull Exception e) {
  23. int statusCode = ((ApiException) e).getStatusCode();
  24. switch (statusCode) {
  25. case CommonStatusCodes.RESOLUTION_REQUIRED:
  26. // Location settings are not satisfied, but this can be fixed
  27. // by showing the user a dialog.
  28. try {
  29. // Show the dialog by calling startResolutionForResult(),
  30. // and check the result in onActivityResult().
  31. ResolvableApiException resolvable = (ResolvableApiException) e;
  32. resolvable.startResolutionForResult(MainActivity.this,
  33. REQUEST_CHECK_SETTINGS);
  34. } catch (IntentSender.SendIntentException sendEx) {
  35. // Ignore the error.
  36. }
  37. break;
  38. case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
  39. // Location settings are not satisfied. However, we have no way
  40. // to fix the settings so we won't show the dialog.
  41. break;
  42. }
  43. }
  44. });

Note: No need to show notication, as location is taken when the app is in foreground.

Background Tracking

Background Tracking is need when you want to track user location even after your app is closed or killed from task. For background tracking you must notify users via foreground notification while getting location. For this purpose we are using foreground service named(LocationJobService.java). Android versions > 7.1 restricts background location process even though foreground service is used. You cannot override this, and you will get 3 0r 4 location updates for 1 hour period. For more information, refer here

Screenshots

main_page
background_service
notification