Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ie.appz.shortestwalkingroute.gps;
- import ie.appz.shortestwalkingroute.sqlite.FixOpenHelper;
- import ie.appz.shortestwalkingroute.sqlite.FixProvider;
- import android.app.Service;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.IBinder;
- public class LocationService extends Service {
- int mRouteNo;
- FixOpenHelper mFixOpenHelper;
- Location lastLocation;
- public static float oldAccuracy = 102;
- LocationManager mLocationManager;
- LocationListener networkListener, gpsListener;
- @Override
- public void onCreate() {
- super.onCreate();
- mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
- networkListener = new LocationListener() {
- public void onLocationChanged(Location location) {
- if (lastLocation == null || (location.getTime() - lastLocation.getTime()) > 3000) {
- addRow(location);
- }
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- public void onProviderEnabled(String provider) {
- }
- public void onProviderDisabled(String provider) {
- }
- };
- gpsListener = new LocationListener() {
- public void onLocationChanged(Location location) {
- addRow(location);
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- public void onProviderEnabled(String provider) {
- }
- public void onProviderDisabled(String provider) {
- }
- };
- mFixOpenHelper = new FixOpenHelper(this);
- }
- @Override
- public void onStart(Intent intent, int startid) {
- mRouteNo = intent.getIntExtra("HIGHESTROUTE", 1);
- /*
- * Register the listener with the Location Manager to receive location
- * updates
- */
- mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 000, 0, networkListener);
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 000, 0, gpsListener);
- /*
- * The two integers in this request are the time (ms) and distance (m)
- * intervals of notifications respectively.
- */
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- protected void addRow(Location location) {
- /*
- * This will reject any GPS fix with very poor accuracy
- */
- if (location.getAccuracy() < 100 && location.getAccuracy() < 2 * oldAccuracy) {
- mFixOpenHelper.addFix(mRouteNo, location);
- lastLocation = location;
- oldAccuracy = (oldAccuracy + location.getAccuracy()) / 2;
- Uri baseUri = Uri.withAppendedPath(FixProvider.CONTENT_URI, FixProvider.ROUTE + "/" + mRouteNo);
- ContentResolver contentResolver = this.getContentResolver();
- contentResolver.notifyChange(baseUri, null);
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- mLocationManager.removeUpdates(networkListener);
- mLocationManager.removeUpdates(gpsListener);
- if (mFixOpenHelper != null) {
- mFixOpenHelper.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement