Java Code Examples for android.location.LocationManager#removeUpdates()

The following examples show how to use android.location.LocationManager#removeUpdates() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HistoryRecord.java    From itag with GNU General Public License v3.0 6 votes vote down vote up
public static void clear(Context context, String addr) {
    if (BuildConfig.DEBUG) Log.d(LT, "clear history" + addr);
    checkRecords(context);
    records.remove(addr);
    save(context);
    LocationListener locationListener = sLocationListeners.get(addr);
    sLocationListeners.remove(addr);
    notifyChange();
    if (locationListener != null) {
        final LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            if (BuildConfig.DEBUG) Log.d(LT, "GPS removeUpdates on clear history" + addr);
            locationManager.removeUpdates(locationListener);
        }
        ITagApplication.faRemovedGpsRequestByConnect();
    }

}
 
Example 2
Source File: BackgroundService.java    From BackPackTrackII with GNU General Public License v3.0 6 votes vote down vote up
public static void stopTracking(final Context context) {
    Log.i(TAG, "Stop tracking");

    stopPeriodicLocating(context);
    stopLocating(context);
    removeStateNotification(context);

    // Cancel activity updates
    stopActivityRecognition(context);

    // Cancel passive location updates
    Intent locationIntent = new Intent(context, BackgroundService.class);
    locationIntent.setAction(BackgroundService.ACTION_LOCATION_PASSIVE);
    PendingIntent pi = PendingIntent.getService(context, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    lm.removeUpdates(pi);

    // Stop step counter
    context.stopService(new Intent(context, StepCounterService.class));
}
 
Example 3
Source File: BackgroundService.java    From BackPackTrackII with GNU General Public License v3.0 6 votes vote down vote up
private void handleSatelliteCheck(Intent intent) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    int fixed = prefs.getInt(SettingsFragment.PREF_SATS_FIXED, 0);
    int visible = prefs.getInt(SettingsFragment.PREF_SATS_VISIBLE, 0);
    int checksat = Integer.parseInt(prefs.getString(SettingsFragment.PREF_CHECK_SAT, SettingsFragment.DEFAULT_CHECK_SAT));
    Log.i(TAG, "Check satellites fixed/visible=" + fixed + "/" + visible + " required=" + checksat);

    // Check if there is any chance for a GPS fix
    if (fixed < checksat) {
        // Cancel fine location updates
        Intent locationIntent = new Intent(this, BackgroundService.class);
        locationIntent.setAction(BackgroundService.ACTION_LOCATION_FINE);
        PendingIntent pi = PendingIntent.getService(this, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.removeUpdates(pi);
        stopService(new Intent(this, GpsStatusService.class));
        Log.i(TAG, "Canceled fine location updates");
    }
}
 
Example 4
Source File: WeathForceActivity.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPause() {
    super.onPause();
    compass.stopOrientationProvider();
    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    try {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        lm.removeUpdates(this);
    } catch (Exception ex) {
    }

    //unlock the orientation
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
 
Example 5
Source File: MainFragment.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onLocationChanged(@NonNull Location location) {
    if (getActivity() != null && (System.currentTimeMillis() - location.getTime()) < (mOnlyNew ? (1000 * 60) : (1000 * 60 * 60 * 24))) {
        LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locMan.removeUpdates(this);
    }

}
 
Example 6
Source File: MainFragment.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (mRefresh == item) {
        mOnlyNew = true;
        if (PermissionUtils.get(getActivity()).pLocation) {
            LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

            locMan.removeUpdates(this);
            List<String> providers = locMan.getProviders(true);
            for (String provider : providers) {
                locMan.requestLocationUpdates(provider, 0, 0, this);
            }
        }
    } else if (mSwitch == item) {
        Preferences.SHOW_COMPASS_NOTE.set(false);
        // compass >> time >> map
        if (mMode == Mode.Map) {
            updateFrag(Mode.Compass);
            mSwitch.setIcon(MaterialDrawableBuilder.with(getActivity()).setIcon(MaterialDrawableBuilder.IconValue.CLOCK).setColor(Color.WHITE)
                    .setToActionbarSize().build());
        } else if (mMode == Mode.Compass) {
            updateFrag(Mode.Time);
            mSwitch.setIcon(MaterialDrawableBuilder.with(getActivity()).setIcon(MaterialDrawableBuilder.IconValue.MAP).setColor(Color.WHITE)
                    .setToActionbarSize().build());
        } else if (mMode == Mode.Time) {
            updateFrag(Mode.Map);
            mSwitch.setIcon(
                    MaterialDrawableBuilder.with(getActivity()).setIcon(MaterialDrawableBuilder.IconValue.COMPASS_OUTLINE).setColor(Color.WHITE)
                            .setToActionbarSize().build());
        } else {
            Toast.makeText(getActivity(), R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
        }
    }

    return super.onOptionsItemSelected(item);
}
 
Example 7
Source File: SalaatAlarmReceiver.java    From PrayTime-Android with Apache License 2.0 5 votes vote down vote up
public void removePassiveLocationUpdates(Context context, PendingIntent pendingIntent) {
  LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  try {
    locationManager.removeUpdates(pendingIntent);
  } catch (SecurityException se) {
    //do nothing. We should always have permision in order to reach this screen.
  }
}
 
Example 8
Source File: LocationHelper.java    From PrayTime-Android with Apache License 2.0 5 votes vote down vote up
public void removePassiveLocationUpdates(Context context, PendingIntent pendingIntent) {
  LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  try {
    locationManager.removeUpdates(pendingIntent);
  } catch (SecurityException se) {
    //do nothing. We should always have permision in order to reach this screen.
  }
}
 
Example 9
Source File: BetterWeatherExtension.java    From BetterWeather with Apache License 2.0 5 votes vote down vote up
/**
 * Disables the location listener
 */
private void disableOneTimeLocationListener() {
    if (mOneTimeLocationListenerActive) {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            LOGE(TAG, "Location permission was not granted, cannot remove location updates");
            return;
        }
        lm.removeUpdates(mOneTimeLocationListener);
        mOneTimeLocationListenerActive = false;
    }
}
 
Example 10
Source File: SampleCustomIconDirectedLocationOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onPause(){
    super.onPause();
    LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    lm.removeUpdates(this);

}
 
Example 11
Source File: MainFragment.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onLocationChanged(@NonNull Location location) {
    if (getActivity() != null && (System.currentTimeMillis() - location.getTime()) < (mOnlyNew ? (1000 * 60) : (1000 * 60 * 60 * 24))) {
        LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locMan.removeUpdates(this);
    }

}
 
Example 12
Source File: GoogleMapImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
@Override
public void setMyLocationEnabled(boolean myLocation) throws RemoteException {
    Log.w(TAG, "MyLocation not yet supported");
    boolean hasPermission = ContextCompat.checkSelfPermission(context, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED;
    if (!hasPermission) {
        throw new SecurityException("Neither " + ACCESS_COARSE_LOCATION + " nor " + ACCESS_FINE_LOCATION + " granted.");
    }
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (myLocation) {
        locationManager.requestLocationUpdates(5000, 10, criteria, listener, Looper.getMainLooper());
    } else {
        locationManager.removeUpdates(listener);
    }
}
 
Example 13
Source File: PlacePickerActivity.java    From Klyph with MIT License 5 votes vote down vote up
@Override
protected void onStop()
{
	super.onStop();

	if (locationListener != null)
	{
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.removeUpdates(locationListener);
		locationListener = null;
	}
}
 
Example 14
Source File: LocationActivity.java    From android with MIT License 5 votes vote down vote up
@Override
protected void onPause() {
    if (mLocationListener != null) {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.removeUpdates(mLocationListener);
    }
    super.onPause();
}
 
Example 15
Source File: LocationModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Stop listening for location updates.
 *
 * NB: this is not balanced with {@link #startObserving}: any number of calls to that method will
 * be canceled by just one call to this one.
 */
@ReactMethod
public void stopObserving() {
  LocationManager locationManager =
      (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  locationManager.removeUpdates(mLocationListener);
  mWatchedProvider = null;
}
 
Example 16
Source File: LocationModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Start listening for location updates. These will be emitted via the
 * {@link RCTDeviceEventEmitter} as {@code geolocationDidChange} events.
 *
 * @param options map containing optional arguments: highAccuracy (boolean)
 */
@ReactMethod
public void startObserving(ReadableMap options) {
  if (LocationManager.GPS_PROVIDER.equals(mWatchedProvider)) {
    return;
  }
  LocationOptions locationOptions = LocationOptions.fromReactMap(options);

  try {
    LocationManager locationManager =
        (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
    if (provider == null) {
      emitError(PositionError.POSITION_UNAVAILABLE, "No location provider available.");
      return;
    }
    if (!provider.equals(mWatchedProvider)) {
      locationManager.removeUpdates(mLocationListener);
      locationManager.requestLocationUpdates(
        provider,
        1000,
        locationOptions.distanceFilter,
        mLocationListener);
    }
    mWatchedProvider = provider;
  } catch (SecurityException e) {
    throwLocationPermissionMissing(e);
  }
}
 
Example 17
Source File: GpsEventReceiver.java    From satstat with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param args[0] A {@link Context} for connecting to the various system services
 * @param args[1] The {@link Intent} to raise when the next update is due
 * @param args[2] A {@link SharedPreferences} instance in which the timestamp of the update will be stored
 * @param args[3] The update frequency, of type {@link Long}, in milliseconds
 */
@Override
protected Integer doInBackground(Object... args) {
	mContext = (Context) args[0];
	Intent agpsIntent = (Intent) args[1];
	SharedPreferences sharedPref = (SharedPreferences) args[2];
	long freqMillis = (Long) args[3];
	
	int nc = WifiCapabilities.getNetworkConnectivity();
	if (nc == WifiCapabilities.NETWORK_CAPTIVE_PORTAL) {
		// portale cattivo che non ci permette di scaricare i dati AGPS
		Log.i(GpsEventReceiver.class.getSimpleName(), "Captive portal detected, cannot update AGPS data");
		return nc;
	} else if (nc == WifiCapabilities.NETWORK_ERROR) {
		Log.i(GpsEventReceiver.class.getSimpleName(), "No network available, cannot update AGPS data");
		return nc;
	}
	
	AlarmManager alm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
	PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, agpsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	alm.cancel(pi);

	LocationManager locman = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
	List<String> allProviders = locman.getAllProviders();
	PendingIntent tempIntent = PendingIntent.getBroadcast(mContext, 0, mLocationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	Log.i(GpsEventReceiver.class.getSimpleName(), "Requesting AGPS data update");
	try {
		if (allProviders.contains(LocationManager.GPS_PROVIDER))
			locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, tempIntent);
		locman.sendExtraCommand("gps", "force_xtra_injection", null);
		locman.sendExtraCommand("gps", "force_time_injection", null);
		locman.removeUpdates(tempIntent);
		
		SharedPreferences.Editor spEditor = sharedPref.edit();
		spEditor.putLong(Const.KEY_PREF_UPDATE_LAST, System.currentTimeMillis());
		spEditor.commit();
	} catch (SecurityException e) {
		Log.w(GpsEventReceiver.class.getSimpleName(), "Permissions not granted, cannot update AGPS data");
	}
	
	if (freqMillis > 0) {
		// if an update interval is set, prepare an alarm to trigger a new
		// update when it elapses (if no interval is set, do nothing as we
		// cannot determine a point in time for re-running the update)
		long next = System.currentTimeMillis() + freqMillis;
		alm.set(AlarmManager.RTC, next, pi);
		Log.i(GpsEventReceiver.class.getSimpleName(), String.format("Next update due %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS (after %2$d ms)", next, freqMillis));
	}

	return nc;
}
 
Example 18
Source File: ThemeActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void stopLocationUpdate() {
    updatingLocation = false;
    LocationManager locationManager = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(gpsLocationListener);
    locationManager.removeUpdates(networkLocationListener);
}
 
Example 19
Source File: DeviceLocation.java    From ARCore-Location with MIT License 4 votes vote down vote up
private void stopUpdatingLocation() {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(this);
    isLocationManagerUpdatingLocation = false;
}
 
Example 20
Source File: ThemeActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void stopLocationUpdate() {
    updatingLocation = false;
    LocationManager locationManager = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(gpsLocationListener);
    locationManager.removeUpdates(networkLocationListener);
}