LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this); boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); if (location != null) { Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude()); mLocationManager.removeUpdates(this);
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); if(network_enabled) lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); timer1=new Timer(); timer1.schedule(new GetLastLocation(), 20000); timer1.cancel(); locationResult.gotLocation(location); lm.removeUpdates(this); lm.removeUpdates(locationListenerNetwork); timer1.cancel(); locationResult.gotLocation(location); lm.removeUpdates(this); lm.removeUpdates(locationListenerGps); gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(network_enabled) net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if(gpsProvider != null) locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 10, this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 5, 0, this); public void deactivate() locationManager.removeUpdates(this);
void addLocationListener() { // Note: Use the Fused Location Provider from Google Play Services instead. // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener); Log.d("BoundLocationMgr", "Listener added"); // Force an update with the last location, if available. Location lastLocation = mLocationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); if (lastLocation != null) { mListener.onLocationChanged(lastLocation); } }
public void setLocationManagerAndListener(){ this.locationListener = new SmartSendLocationListener(this.ctx); this.locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); isGPSProviderEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); isNetworkProviderEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if(isGPSProviderEnable){ this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTimeForUpdateLocastion, minDistanceForUpdateLocation, locationListener); }else if(isNetworkProviderEnable){ this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTimeForUpdateLocastion, minDistanceForUpdateLocation, locationListener); } }
private void _getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(bestProvider); LocationListener loc_listener = new LocationListener() { public void onLocationChanged(Location l) {} public void onProviderEnabled(String p) {} public void onProviderDisabled(String p) {} public void onStatusChanged(String p, int status, Bundle extras) {} }; locationManager .requestLocationUpdates(bestProvider, 0, 0, loc_listener); location = locationManager.getLastKnownLocation(bestProvider); try { lat = location.getLatitude(); lon = location.getLongitude(); } catch (NullPointerException e) { lat = -1.0; lon = -1.0; } }
this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); updateCoordinates(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); updateCoordinates();
@Override public void onLocationChanged(Location location) { // thread is not runable, msg ignore, state:TIMED_WAITING, 这里的线程有可能ANR if (location != null) { double lat = location.getLatitude();//获取纬度 double lng = location.getLongitude();//获取经度 System.out.println("shiming lat+" + lat); System.out.println("shiming lng+" + lng); String name = Thread.currentThread().getName(); mCount++; System.out.println("当前线程的位置name---"+name+"i==="+mCount); mTv_location.setText("位置信息是2s变动的,可以设置,我是第"+mCount+"次变动的--->"+"\n\r"+"lat===="+lat+" lng----->"+lng); } if (mLocationManager!=null) { mLocationManager.removeUpdates(this); if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 1000, mLlistener); }else { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1000, mLlistener); } // TODO: 2018/5/3 这里在报错了,我把他注释掉 // mLocationManager.setTestProviderEnabled(mProvider, false);// java.lang.IllegalArgumentException: Provider "network" unknown } }
private Location getFreshLocation() throws ScannerException { freshLocation = null; HandlerThread handlerThread = new HandlerThread("LocationScanner"); try { handlerThread.start(); for (String provider : enabledProviders) { locationManager.requestLocationUpdates( provider, MIN_TIME_BETWEEN_UPDATES, MIN_DISTANCE_BETWEEN_UPDATES, this, handlerThread.getLooper()); } try { synchronized (scanLock) { scanLock.wait(params.getLocationRequestTimeoutMs()); } } catch (Exception e) { // ignore } } finally { locationManager.removeUpdates(this); handlerThread.quit(); } if (freshLocation == null) { throw new ScannerException(ScannerException.Type.TIMEOUT); } return freshLocation; }
super.onCreate(); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); sendBroadcastMessage(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, new LocationListener() { @Override
public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); .isProviderEnabled(LocationManager.GPS_PROVIDER); .isProviderEnabled(LocationManager.NETWORK_PROVIDER); this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, locationManager.removeUpdates(GPSTracker.this);
public void FindLocation(Context context){ final LocationManager locationManager (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener(){ public void onLocationChanged(Location location){ updateLocation(location); locationManager.removeUpdates(this); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); }
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) void addLocationListener() { // Note: Use the Fused Location Provider from Google Play Services instead. // https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener); Log.d("BoundLocationMgr", "Listener added"); // Force an update with the last location, if available. Location lastLocation = mLocationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); if (lastLocation != null) { mListener.onLocationChanged(lastLocation); } }
public boolean getLocation(final Context context, final LocationResult result) { //I use LocationResult callback class to pass location value from CurrentLocationHelper to user code. mLocationResult = result; if (mLocationManager == null) mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //exceptions will be thrown if provider is not permitted. try { mGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (final Exception ex) { } try { mNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(final Exception ex){ } //don't start listeners if no provider is enabled if (!mGpsEnabled && !mNetworkEnabled) return false; if (mGpsEnabled) mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); if (mNetworkEnabled) mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); mTimer = new Timer(); mTimer.schedule(new GetLastLocation(), 20000); return true; }
boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this); locationManager.removeUpdates(this); mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
Location location = locationManager.getLastKnownLocation(provider); protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this); protected void onPause() { super.onPause(); locationManager.removeUpdates(this);
gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener); myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener); myLocationManager.addGpsStatusListener(gpsStatusListener); Location location = lm.getLastKnownLocation(strLocationProvider); if(location != null){ return location;
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { protected void onStart() { super.onStart(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); locationManager.removeUpdates(this); super.onStop();