android.location.LocationManager类的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(14.3k)|赞(0)|评价(0)|浏览(304)

本文整理了Java中android.location.LocationManager类的一些代码示例,展示了LocationManager类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocationManager类的具体详情如下:
包路径:android.location.LocationManager
类名称:LocationManager

LocationManager介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

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);
LocationListener locationListenerGps = new LocationListener() {
  public void onLocationChanged(Location location) {
    timer1.cancel();
    locationResult.gotLocation(location);
    lm.removeUpdates(this);
    lm.removeUpdates(locationListenerNetwork);
LocationListener locationListenerNetwork = new LocationListener() {
  public void onLocationChanged(Location location) {
    timer1.cancel();
    locationResult.gotLocation(location);
    lm.removeUpdates(this);
    lm.removeUpdates(locationListenerGps);
  @Override
  public void run() {
     lm.removeUpdates(locationListenerGps);
     lm.removeUpdates(locationListenerNetwork);
       gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

代码示例来源:origin: stackoverflow.com

private final Criteria criteria = new Criteria();
private String bestAvailableProvider;
  locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  criteria.setAccuracy(Criteria.ACCURACY_FINE);
  criteria.setPowerRequirement(Criteria.POWER_LOW);
  criteria.setAltitudeRequired(true);
  criteria.setBearingRequired(true);
  bestAvailableProvider = locationManager.getBestProvider(criteria, true);
    locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this);
  } else {
public void deactivate() {
  locationManager.removeUpdates(this);
  mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));

代码示例来源:origin: stackoverflow.com

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);

Location locations = locationManager.getLastKnownLocation(provider);
List<String>  providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){                 
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());                 
try {
  List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
  if(null!=listAddresses&&listAddresses.size()>0){
    String _Location = listAddresses.get(0).getAddressLine(0);
  }
} catch (IOException e) {
  e.printStackTrace();
}

}

代码示例来源:origin: stackoverflow.com

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

try {
     Log.d(TAG ,"Removing Test providers")
     lm.removeTestProvider(LocationManager.GPS_PROVIDER);
   } catch (IllegalArgumentException error) {
     Log.d(TAG,"Got exception in removing test  provider");
   }

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);

代码示例来源:origin: stackoverflow.com

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
  map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

  CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
  map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
}

代码示例来源:origin: stackoverflow.com

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;
  }
}

代码示例来源:origin: stackoverflow.com

.getSystemService(Context.LOCATION_SERVICE);
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
      5000, 5, listener);
private LocationListener listener = new LocationListener() {
        Log.e("latitude", location.getLatitude() + "");
        Log.e("longitude", location.getLongitude() + "");
        jsonObject.put("latitude", location.getLatitude());
        jsonObject.put("longitude", location.getLongitude());
      .getSystemService(Context.CONNECTIVITY_SERVICE);
  if (connectivity != null) {
    NetworkInfo[] info = connectivity.getAllNetworkInfo();

代码示例来源:origin: stackoverflow.com

_instance = new GPSTracker();
_locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
_isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
_isNetworkEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!_isGPSEnabled && !_isNetworkEnabled) {
  this._canGetLocation = true;
  if (_isNetworkEnabled) {
    _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    Log.d("Network", "Network");
    if (_locationManager != null) {
      _location = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      if (_location != null) {
        _latitude = _location.getLatitude();
        _longitude = _location.getLongitude();
      _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      Log.d("GPS Enabled", "GPS Enabled");
      if (_locationManager != null) {
        _location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (_location != null) {
          _latitude = _location.getLatitude();
          _longitude = _location.getLongitude();
_locationManager.removeUpdates(GPSTracker.this);
  _context.startActivity(intent);

代码示例来源:origin: stackoverflow.com

LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
  network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  dialog.setMessage("Location Services Disabled. \n Please enable location services.");
  dialog.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface paramDialogInterface, int paramInt) {
        Intent myIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(myIntent);
  dialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {

代码示例来源:origin: stackoverflow.com

getLocation();
  locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
  isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    locationManager.requestLocationUpdates(
      provider_info,
      MIN_TIME_BW_UPDATES,
      location = locationManager.getLastKnownLocation(provider_info);
      updateGPSCoordinates();
  locationManager.removeUpdates(GPSTracker.this);
alertDialog.setTitle(R.string.GPSAlertDialogTitle);
alertDialog.setMessage(R.string.GPSAlertDialogMessage);
alertDialog.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener() {
    mContext.startActivity(intent);
List<Address> addresses = getGeocoderAddress(context);

代码示例来源:origin: stackoverflow.com

getLocation();
try {
  locationManager = (LocationManager) mContext
      .getSystemService(LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER,
            MIN_TIME_BW_UPDATES,
              .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

代码示例来源:origin: stackoverflow.com

public LatLng getLocation(Context ctx) 
{
  LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
  List<String> providers = lm.getProviders(true);

  /*
   * Loop over the array backwards, and if you get an accurate location,
   * then break out the loop
   */
  Location l = null;

  for (int i = providers.size() - 1; i >= 0; i--) 
  {
    l = lm.getLastKnownLocation(providers.get(i));
    if (l != null)
      break;
  }
  return new LatLng(l.getLatitude(),l.getLongitude());
}

代码示例来源:origin: joyoyao/superCleanMaster

/**
 * Gps是否打开 需要<uses-permission
 * android:name="android.permission.ACCESS_FINE_LOCATION" />权限
 *
 * @param context the context
 * @return true, if is gps enabled
 */
public static boolean isGpsEnabled(Context context) {
  LocationManager lm = (LocationManager) context
      .getSystemService(Context.LOCATION_SERVICE);
  return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

代码示例来源:origin: stackoverflow.com

Log.i(TAG, "Boot : registered for location updates");
  LocationManager lm = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
  Intent intent = new Intent(context, this.getClass());
  PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,5,pi);
} else {
  String locationKey = LocationManager.KEY_LOCATION_CHANGED;
      db.open();
      String android_id = Secure.getString(
        context.getContentResolver(), Secure.ANDROID_ID);
      Log.i(TAG, "Android id is :" + android_id);
      db.insertGPSCoordinates(android_id,
        Double.toString(loc.getLatitude()),
        Double.toString(loc.getLongitude()));
    } catch (Exception e) { // NEVER catch generic "Exception"
      Log.i(TAG, "db error catch :" + e.getMessage());

代码示例来源:origin: stackoverflow.com

locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);
      gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (networkEnabled)
      networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
private final LocationListener locationListenerGps = new LocationListener() {
  public void onLocationChanged(Location location) {              
    timer.cancel();
    locationResult.gotLocation(location);
    locationManager.removeUpdates(this);
    locationManager.removeUpdates(locationListenerNetwork);
private final LocationListener locationListenerNetwork = new LocationListener() {
  public void onLocationChanged(Location location) {    
    timer.cancel(); 
    locationResult.gotLocation(location);
    locationManager.removeUpdates(this);
    locationManager.removeUpdates(locationListenerGps);
    gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  } catch (Exception ex) {
    networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  } catch (Exception ex) {
    locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListenerGps, Looper.myLooper());

代码示例来源:origin: stackoverflow.com

final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
  Criteria criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_COARSE);
  locationManager.requestSingleUpdate(criteria, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
  }, null);
} else {
  boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  if (isGPSEnabled) {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationManager.requestSingleUpdate(criteria, new LocationListener() {
      @Override
      public void onLocationChanged(Location location) {

代码示例来源:origin: stackoverflow.com

LocationManager locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String locProvider = locMgr.getBestProvider(criteria, false);
Location location = locMgr.getLastKnownLocation(locProvider);
boolean isGPSEnabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNWEnabled = locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      location = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location == null)
      if (locMgr != null)
        location = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return new LatLng(location.getLatitude(), location.getLongitude());
return new LatLng(0, 0);
return new LatLng(0, 0);

代码示例来源:origin: stackoverflow.com

getLocation();
try {
  locationManager = (LocationManager) mContext
      .getSystemService(LOCATION_SERVICE);
      .isProviderEnabled(LocationManager.GPS_PROVIDER);
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  locationManager.removeUpdates(GPSTracker.this);
  latitude = location.getLatitude();
  longitude = location.getLongitude();

代码示例来源:origin: stackoverflow.com

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 40, this);
Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
  location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null) {
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();
  LatLng currentLatLng = new LatLng(latitude, longitude);
NetworkInfo ni = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
return (ni!=null && ni.isAvailable() && ni.isConnected());

代码示例来源:origin: stackoverflow.com

mContext = context;
locationClient = new LocationClient(mContext, this, this);
configureLocationRequest();
connectToApi();
    mLocation = getLastKnownLocationWithDeprecatedApi();
  }else if (isCurrentLocationEqualsTodefaultGPSLocation()){
LocationManager locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;

相关文章