Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.chulakov.ris.utils;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.os.IBinder;
- /**
- * Service to getting user location.
- */
- public class GPSTracker extends Service
- {
- private LocationManager mLocationManager = null;
- private static final int LOCATION_INTERVAL = 1000 * 10;
- private static final int LOCATION_DISTANCE = 100;
- public final static int ACCURACY = 25;
- /** Constant to transmission latitude through broadcast. */
- public static final String LATITUDE = "latitude";
- /** Constant to transmission longitude through broadcast. */
- public static final String LONGITUDE = "longitude";
- /** Intent action for work with broadcast. */
- public static final String INTENT_ACTION = "GPSLocationUpdates";
- private boolean isUsedGPS;
- /**
- * Sends location to activity via broadcast.
- *
- * @param location Location passed to Activity.
- */
- private void sendMessageToActivity(Location location) {
- Intent intent = new Intent(INTENT_ACTION);
- Bundle b = new Bundle();
- b.putDouble(LATITUDE, location.getLatitude());
- b.putDouble(LONGITUDE, location.getLongitude());
- intent.putExtras(b);
- sendBroadcast(intent);
- }
- private class LocationListener implements android.location.LocationListener{
- @Override
- public void onLocationChanged(Location location) {
- sendMessageToActivity(location);
- if (isUsedGPS && location.getAccuracy() < ACCURACY)
- disableGPS();
- }
- @Override
- public void onProviderDisabled(String provider) {}
- @Override
- public void onProviderEnabled(String provider) {}
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {}
- }
- final LocationListener mLocationListenerNetwork = new LocationListener();
- final LocationListener mLocationListenerGPS = new LocationListener();
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- private static final String TAG = GPSTracker.class.getSimpleName();
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- super.onStartCommand(intent, flags, startId);
- if (PermissionUtils.appHasAccessFineLocationPermission(getApplicationContext())) {
- requestUpdatesIfAvailable(LocationManager.NETWORK_PROVIDER,
- LOCATION_INTERVAL,
- LOCATION_DISTANCE,
- mLocationListenerNetwork);
- if (isUsedGPS)
- requestUpdatesIfAvailable(LocationManager.GPS_PROVIDER,
- LOCATION_INTERVAL,
- LOCATION_DISTANCE,
- mLocationListenerGPS);
- }
- return START_STICKY;
- }
- @Override
- public void onCreate() {
- isUsedGPS = true;
- initializeLocationManager();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- //noinspection MissingPermission
- if (mLocationManager != null) {
- mLocationManager.removeUpdates(mLocationListenerGPS);
- mLocationManager.removeUpdates(mLocationListenerNetwork);
- }
- }
- private void disableGPS() {
- isUsedGPS = false;
- mLocationManager.removeUpdates(mLocationListenerGPS);
- }
- private void initializeLocationManager() {
- if (mLocationManager == null)
- mLocationManager = (LocationManager) getApplicationContext()
- .getSystemService(Context.LOCATION_SERVICE);
- }
- /**
- * Requests location updates if provider is available.
- *
- * @param provider Location provider.
- * @param locationInterval Interval between updates.
- * @param locationDistance Distance between updates.
- * @param locationListener Callback function.
- */
- private void requestUpdatesIfAvailable(String provider,
- int locationInterval,
- int locationDistance,
- LocationListener locationListener)
- {
- if (!mLocationManager.isProviderEnabled(provider))
- return;
- //noinspection MissingPermission
- mLocationManager.requestLocationUpdates(provider,
- locationInterval,
- locationDistance,
- locationListener);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement