Android Open Source - GPS_Receiver Location Provider Service






From Project

Back to project page GPS_Receiver.

License

The source code is released under:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute v...

If you think the Android project GPS_Receiver listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Intellectual properties of Supun Lakshan Wanigarathna Dissanayake
 * Copyright (c) 2014, Supun Lakshan Wanigarathna Dissanayake. All rights reserved.
 * Created on : Oct 17, 2014, 8:45 PM//from w w w.  ja v  a 2s. c o m
 */
package org.xfinity.gps_receiver.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import com.squareup.otto.Produce;
import org.xfinity.gps_receiver.util.BusProvider;

/**
 * @author Supun Lakshan Wanigarathna Dissanayake
 * @mobile +94711290392
 * @email supunlakshan.xfinity@gmail.com
 */
public class LocationProviderService extends Service {

  private final long MINIMUM_TIME_DIFFERENCE = 0;
  private final float MINIMUM_DISTANCE_CHANGE = 0;
  private LocationManager locationManager;
  private LocationListener locationListener;
  private boolean satellite_gps_enabled = true;
  private boolean network_gps_enabled = true;
  private LocationChangedEvent locationChangedEvent = new LocationChangedEvent();

  @Override
  public void onCreate() {
    super.onCreate();
    //initializing
    BusProvider.getInstance().register(LocationProviderService.this);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
      @Override
      public void onLocationChanged(Location location) {
        if (location != null) {
          locationChangedEvent.location = location;
          BusProvider.getInstance().post(locationChangedEvent);
        }
      }

      @Override
      public void onProviderDisabled(String provider) {
      }

      @Override
      public void onProviderEnabled(String provider) {
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
    };

    //request location updates from satellites
    if (satellite_gps_enabled) {
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_DIFFERENCE, MINIMUM_DISTANCE_CHANGE, locationListener, Looper.getMainLooper());
      locationChangedEvent.location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }

    //request location updates from network
    if (network_gps_enabled) {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_DIFFERENCE, MINIMUM_DISTANCE_CHANGE, locationListener, Looper.getMainLooper());
      locationChangedEvent.location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }


  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    locationManager.removeUpdates(locationListener);
    BusProvider.getInstance().unregister(LocationProviderService.this);
    super.onDestroy();
  }

  @Produce
  public LocationChangedEvent broadcastLocation() {
    return locationChangedEvent;
  }

  public static class LocationChangedEvent {
    private Location location;

    private LocationChangedEvent() {
    }

    public Location getLocation() {
      return location;
    }
  }
}




Java Source Code List

org.xfinity.gps_receiver.activity.StartingPoint.java
org.xfinity.gps_receiver.service.LocationProviderService.java
org.xfinity.gps_receiver.util.BusProvider.java