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

The following examples show how to use android.location.LocationManager#getProviders() . 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: MainFragment.java    From prayer-times-android with Apache License 2.0 7 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    if (PermissionUtils.get(getActivity()).pLocation) {
        LocationManager locMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        List<String> providers = locMan.getProviders(true);
        for (String provider : providers) {
            locMan.requestLocationUpdates(provider, 0, 0, this);
            Location lastKnownLocation = locMan.getLastKnownLocation(provider);
            if (lastKnownLocation != null) {
                calcQiblaAngle(lastKnownLocation);
            }
        }
    }

    if (Preferences.COMPASS_LAT.get() != 0) {
        Location loc = new Location("custom");
        loc.setLatitude(Preferences.COMPASS_LAT.get());
        loc.setLongitude(Preferences.COMPASS_LNG.get());
        calcQiblaAngle(loc);
    }
}
 
Example 2
Source File: ChatAttachAlertLocationLayout.java    From Telegram-FOSS with GNU General Public License v2.0 7 votes vote down vote up
private Location getLastLocation() {
    if (Build.VERSION.SDK_INT >= 23 && getParentActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && getParentActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return null;
    } else {
        LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = lm.getProviders(true);
        Location l = null;
        for (int i = providers.size() - 1; i >= 0; i--) {
            l = lm.getLastKnownLocation(providers.get(i));
            if (l != null) {
                break;
            }
        }
        return l;
    }
}
 
Example 3
Source File: ChronometerView.java    From open-quartz with Apache License 2.0 6 votes vote down vote up
public ChronometerView(Context context) {
    this(context, null, 0);

    final StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    geocoder = new Geocoder(context);

    criteria = new Criteria();
    best = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(best);

    providers = locationManager.getProviders(criteria, true);

    for (String provider : providers) {
        locationManager.requestLocationUpdates(provider, 0, 0, this);
        Log.e("TAG", "" + provider);
    }

}
 
Example 4
Source File: Address.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
public Location getLastKnownCoarseLocation () {
    LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    try {
        // Walk through each enabled location provider and return the first found, last-known location
        for (String thisLocProvider : locationManager.getProviders(true)) {
            Location lastKnown = locationManager.getLastKnownLocation(thisLocProvider);

            if (lastKnown != null) {
                return lastKnown;
            }
        }
    } catch(SecurityException exception) {

    }
    // Always possible there's no means to determine location
    return null;
}
 
Example 5
Source File: LocationFineTest.java    From AndPermission with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test() throws Throwable {
    LocationManager locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    boolean gpsProvider = providers.contains(LocationManager.GPS_PROVIDER);
    boolean passiveProvider = providers.contains(LocationManager.PASSIVE_PROVIDER);
    if (gpsProvider || passiveProvider) {
        return true;
    }

    PackageManager packageManager = mContext.getPackageManager();
    boolean gpsHardware = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
    if (!gpsHardware) return true;

    return !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 6
Source File: MapActivity.java    From nongbeer-mvp-android-demo with Apache License 2.0 6 votes vote down vote up
private Location getLastKnownLocation(){
    if( ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ){
        return null;
    }
    LocationManager locationManager =
            (LocationManager) this.getSystemService( LOCATION_SERVICE );
    List<String> providers = locationManager.getProviders( true );
    Location bestLocation = null;
    for( String provider : providers ){
        Location l = locationManager.getLastKnownLocation( provider );
        if( l == null ){
            continue;
        }
        if( bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy() ){
            bestLocation = l; // Found best last known location;
        }
    }
    return bestLocation;
}
 
Example 7
Source File: LocationHelper.java    From RxAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
public static Location getLastKnownLocation(Context context) {
    LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}
 
Example 8
Source File: LocationActivity.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private Location getLastLocation() {
    if (Build.VERSION.SDK_INT >= 23 && getParentActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && getParentActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return null;
    } else {
        LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = lm.getProviders(true);
        Location l = null;
        for (int i = providers.size() - 1; i >= 0; i--) {
            l = lm.getLastKnownLocation(providers.get(i));
            if (l != null) {
                break;
            }
        }
        return l;
    }
}
 
Example 9
Source File: MapsforgeActivity.java    From WhereYouGo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Centers the map to the last known position as reported by the most accurate location provider.
 * If the last location is unknown, a toast message is displayed instead.
 */
private void gotoLastKnownPosition() {
    Location bestLocation = null;
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    for (String provider : locationManager.getProviders(true)) {
        Location currentLocation = locationManager.getLastKnownLocation(provider);
        if (currentLocation == null)
            continue;
        if (bestLocation == null || bestLocation.getAccuracy() > currentLocation.getAccuracy()) {
            bestLocation = currentLocation;
        }
    }

    // check if a location has been found
    if (bestLocation != null) {
        GeoPoint geoPoint = new GeoPoint(bestLocation.getLatitude(), bestLocation.getLongitude());
        this.mapView.getMapViewPosition().setCenter(geoPoint);
    } else {
        showToastOnUiThread(getString(R.string.error_last_location_unknown));
    }
}
 
Example 10
Source File: BaseActivity.java    From JsBridge with Apache License 2.0 6 votes vote down vote up
public Location getLocation(LocationListener listener) {
    locationListener = listener;
    mLocationManager = (LocationManager) HiApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    String locationProvider = null;
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
        //如果是GPS
        locationProvider = LocationManager.GPS_PROVIDER;
    } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        //如果是Network
        locationProvider = LocationManager.NETWORK_PROVIDER;
    }
    Location location = mLocationManager.getLastKnownLocation(locationProvider);
    mLocationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
    return location;
}
 
Example 11
Source File: LocationCoarseTester.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    LocationManager locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    boolean networkProvider = providers.contains(LocationManager.NETWORK_PROVIDER);
    if (networkProvider) {
        return true;
    }

    PackageManager packageManager = mContext.getPackageManager();
    boolean networkHardware = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK);
    if (!networkHardware) return true;

    return !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
 
Example 12
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 5 votes vote down vote up
@ReactMethod(isBlockingSynchronousMethod = true)
public WritableMap getAvailableLocationProvidersSync() {
  LocationManager mLocationManager = (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  WritableMap providersAvailability = Arguments.createMap();
  try {
    List<String> providers = mLocationManager.getProviders(false);
    for (String provider : providers) {
      providersAvailability.putBoolean(provider, mLocationManager.isProviderEnabled(provider));
    }
  } catch (Exception e) {
    System.err.println("Unable to get location providers. LocationManager was null");
  }

  return providersAvailability;
}
 
Example 13
Source File: ChatAttachAlertLocationLayout.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private Location getLastLocation() {
    LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);
    Location l = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null) {
            break;
        }
    }
    return l;
}
 
Example 14
Source File: Util.java    From snowplow-android-tracker with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the location of the android
 * device.
 *
 * @param context the android context
 * @return the phones Location
 */
public static Location getLastKnownLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location location = null;

    try {
        String locationProvider = null;
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            List<String> locationProviders = locationManager.getProviders(true);
            if (locationProviders.size() > 0) {
                locationProvider = locationProviders.get(0);
            }
        }

        if (locationProvider != null && !locationProvider.equals("")) {
            location = locationManager.getLastKnownLocation(locationProvider);
        }
    } catch (SecurityException ex) {
        Logger.e(TAG, "Exception occurred when retrieving location: %s", ex.toString());
    }

    return location;
}
 
Example 15
Source File: LocationService.java    From GracefulMovies with Apache License 2.0 5 votes vote down vote up
private void initLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (mLocationManager == null)
        return;

    List<String> providers = mLocationManager.getProviders(true);

    String locationProvider;
    /**
     * 如果首选GPS定位,会存在这种情况,上次GPS启动采集数据在A地,本次在B地需要定位,但用户恰好在室内无
     * GPS信号,只好使用上次定位数据,就出现了地区级偏差。而网络定位则更具有实时性,在精度要求不高以及室内
     * 使用场景更多的前提下,首选网络定位
     */
    if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        locationProvider = LocationManager.NETWORK_PROVIDER; // 首选网络定位
    } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
        locationProvider = LocationManager.GPS_PROVIDER;
    } else {
        locationProvider = LocationManager.PASSIVE_PROVIDER;
    }

    if (mLocationListener != null)
        mLocationManager.requestLocationUpdates(locationProvider, 2000, 10, mLocationListener);
}
 
Example 16
Source File: SystemUtil.java    From KeyboardView with Apache License 2.0 5 votes vote down vote up
/**
 * 检测GPS是否打开
 *
 * @param context
 * @return
 */
public static boolean isGpsEnabled(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> accessibleProviders = lm.getProviders(true);
    for (String name : accessibleProviders) {
        if ("gps".equals(name)) {
            return true;
        }
    }
    return false;
}
 
Example 17
Source File: LocationActivity.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private Location getLastLocation() {
    LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);
    Location l = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null) {
            break;
        }
    }
    return l;
}
 
Example 18
Source File: GeofenceReceiver.java    From android-sdk with MIT License 5 votes vote down vote up
private boolean isLocationEnabled(LocationManager locationManager) {
    for (String provider : locationManager.getProviders(true)) {
        if (LocationManager.GPS_PROVIDER.equals(provider)
                || LocationManager.NETWORK_PROVIDER.equals(provider)) {
            return true;
        }
    }
    return false;
}
 
Example 19
Source File: LocationCoarseTest.java    From AndPermission with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    LocationManager locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    boolean networkProvider = providers.contains(LocationManager.NETWORK_PROVIDER);
    if (networkProvider) {
        return true;
    }

    PackageManager packageManager = mContext.getPackageManager();
    boolean networkHardware = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK);
    if (!networkHardware) return true;

    return !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
 
Example 20
Source File: LocationUtils.java    From XPermissionUtils with Apache License 2.0 4 votes vote down vote up
private static void startLocation(Context context) {
    //获取地理位置管理器
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    //获取所有可用的位置提供器
    List<String> providers = locationManager.getProviders(true);
    if (providers == null) {
        return;
    }

    //获取Location
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    //对提供者进行排序,gps、net、passive
    List<String> providerSortList = new ArrayList<>();
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
        Log.d(TAG, "GPS_PROVIDER");
        providerSortList.add(LocationManager.GPS_PROVIDER);
    }
    if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        Log.d(TAG, "NETWORK_PROVIDER");
        providerSortList.add(LocationManager.NETWORK_PROVIDER);
    }
    if (providers.contains(LocationManager.PASSIVE_PROVIDER)) {
        Log.d(TAG, "PASSIVE_PROVIDER");
        providerSortList.add(LocationManager.PASSIVE_PROVIDER);
    }
    String locationProvider = "";
    for (int i = 0; i < providerSortList.size(); i++) {
        String provider = providerSortList.get(i);
        Log.d(TAG, "正在尝试:" + provider);
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            locationProvider = provider;
            Log.d(TAG, "定位成功:" + provider);
            saveLocation(location);
            break;
        } else {
            Log.d(TAG, "定位失败:" + provider);
        }
    }
    if (!TextUtils.isEmpty(locationProvider)) {
        locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
    }
}