Example usage for android.location LocationManager NETWORK_PROVIDER

List of usage examples for android.location LocationManager NETWORK_PROVIDER

Introduction

In this page you can find the example usage for android.location LocationManager NETWORK_PROVIDER.

Prototype

String NETWORK_PROVIDER

To view the source code for android.location LocationManager NETWORK_PROVIDER.

Click Source Link

Document

Name of the network location provider.

Usage

From source file:com.prey.services.LocationService.java

@Override
public void onCreate() {
    PreyLogger.d("LocationService is going to be started...");

    androidLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {

        LocationProvider gpsLocationProvider = androidLocationManager.getProvider(LocationManager.GPS_PROVIDER);
        LocationProvider networkProvider = androidLocationManager.getProvider(LocationManager.NETWORK_PROVIDER);
        if (gpsLocationProvider != null
                && androidLocationManager.isProviderEnabled(gpsLocationProvider.getName())) {
            androidLocationManager.requestLocationUpdates(gpsLocationProvider.getName(),
                    PreyConfig.UPDATE_INTERVAL, PreyConfig.LOCATION_PROVIDERS_MIN_REFRESH_DISTANCE,
                    gpsLocationListener);
            PreyLogger.d("GPS Location provider has been started.");
        }//from   w  ww. jav  a 2 s  . c  o m
        if (networkProvider != null && androidLocationManager.isProviderEnabled(networkProvider.getName())) {
            androidLocationManager.requestLocationUpdates(networkProvider.getName(),
                    PreyConfig.UPDATE_INTERVAL / 4, PreyConfig.LOCATION_PROVIDERS_MIN_REFRESH_DISTANCE,
                    networkLocationListener);
            PreyLogger.d("NETWORK Location provider has been started.");
        }
    } else {
        PreyLogger.i("___________ask for permission LocationService ACCESS_FINE_LOCATION");
    }

    PreyLogger.d("LocationService has been started...");
}

From source file:com.hoccer.api.android.LinccLocationManager.java

public LinccLocationManager(Context pContext, AsyncLinccer linccer, Updateable updater) {
    mContext = pContext;//from   w w  w . j a  v a  2  s . c  o  m

    mLinccer = linccer;
    mUpdater = updater;

    mLocationManager = (LocationManager) pContext.getSystemService(Context.LOCATION_SERVICE);
    mWifiManager = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);

    mNetworkProviderAvailable = mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER);
}

From source file:org.apache.cordova.core.GeoBroker.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action       The action to execute.
 * @param args       JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return          True if the action was valid, or false if not.
 *//*from   w  w  w  .ja  v  a 2 s. com*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (this.locationManager == null) {
        this.locationManager = (LocationManager) this.cordova.getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
        this.networkListener = new NetworkListener(this.locationManager, this);
        this.gpsListener = new GPSListener(this.locationManager, this);
    }

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

        if (action.equals("getLocation")) {
            boolean enableHighAccuracy = args.getBoolean(0);
            int maximumAge = args.getInt(1);
            Location last = this.locationManager.getLastKnownLocation(
                    (enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER));
            // Check if we can use lastKnownLocation to get a quick reading and use less battery
            if (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) {
                PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last));
                callbackContext.sendPluginResult(result);
            } else {
                this.getCurrentLocation(callbackContext, enableHighAccuracy, args.optInt(2, 60000));
            }
        } else if (action.equals("addWatch")) {
            String id = args.getString(0);
            boolean enableHighAccuracy = args.getBoolean(1);
            this.addWatch(id, callbackContext, enableHighAccuracy);
        } else if (action.equals("clearWatch")) {
            String id = args.getString(0);
            this.clearWatch(id);
        } else {
            return false;
        }
    } else {
        PluginResult.Status status = PluginResult.Status.NO_RESULT;
        String message = "Location API is not available for this device.";
        PluginResult result = new PluginResult(status, message);
        callbackContext.sendPluginResult(result);
    }
    return true;
}

From source file:org.pjeremie.floodinfo.GPSTracker.java

public Location getLocation() {
    try {//from   w  ww  .j  a  v a2  s .c  o  m
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            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();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    if (ActivityCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                            && ActivityCompat.checkSelfPermission(this,
                                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        showSettingsAlert();
                        return null;
                    }
                    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();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

From source file:a14n.geolocationdemo.MainActivity.java

private void requestLocationUpdates() {
    // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            JSONObject locationAsJson = new JSONObject();
            try {
                locationAsJson.put("accuracy", location.getAccuracy());
                locationAsJson.put("provider", location.getProvider());
                locationAsJson.put("latitude", location.getLatitude());
                locationAsJson.put("longitude", location.getLongitude());
                locationAsJson.put("time", location.getTime());
            } catch (JSONException e) {
                Log.e(TAG, "JSON exception", e);
                return;
            }/*w  w w . j av a2 s.  c  o  m*/
            flutterView.sendToFlutter("locations", locationAsJson.toString(), null);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

From source file:de.tu_darmstadt.kom.freifunkfinder.user_interface.MobileLocationManager.java

/**
 * Check and register location providers
 * Reference http://stackoverflow.com/questions/2227292
 *//* w  ww . j  av a 2  s .  c o m*/
public void initLocation() {

    //get both provider status
    try {
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    try {
        networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    //open settings in case location is not enabled
    if (!gpsEnabled && !networkEnabled && !onceCalled) {
        onceCalled = true;
        buildAlertMessageNoGpsNet();
    }

    if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(applicationContext,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    //start GPS updates
    if (gpsEnabled) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 0, locationListenerForGps);
    }

    //start Network updates
    if (networkEnabled) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 50, 0,
                locationListenerForNetwork);
    }
}

From source file:org.skt.runtime.original.GeoBroker.java

/**
 * Executes the request and returns PluginResult.
 * /*from   w ww . j a  v a  2 s .c  om*/
 * @param action       The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId   The callback id used when calling back into JavaScript.
 * @return             A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    if (this.locationManager == null) {
        this.locationManager = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE);
        this.networkListener = new NetworkListener(this.locationManager, this);
        this.gpsListener = new GPSListener(this.locationManager, this);
    }
    PluginResult.Status status = PluginResult.Status.NO_RESULT;
    String message = "";
    PluginResult result = new PluginResult(status, message);
    result.setKeepCallback(true);

    try {
        if (action.equals("getLocation")) {
            boolean enableHighAccuracy = args.getBoolean(0);
            int maximumAge = args.getInt(1);
            Location last = this.locationManager.getLastKnownLocation(
                    (enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER));
            // Check if we can use lastKnownLocation to get a quick reading and use less battery
            if ((System.currentTimeMillis() - last.getTime()) <= maximumAge) {
                result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last));
            } else {
                this.getCurrentLocation(callbackId, enableHighAccuracy);
            }
        } else if (action.equals("addWatch")) {
            String id = args.getString(0);
            boolean enableHighAccuracy = args.getBoolean(1);
            this.addWatch(id, callbackId, enableHighAccuracy);
        } else if (action.equals("clearWatch")) {
            String id = args.getString(0);
            this.clearWatch(id);
        }
    } catch (JSONException e) {
        result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
    }
    return result;
}

From source file:com.crowdpp.nagisa.crowdpp2.util.LocationTracker.java

public Location getLocation() {
    try {// w  w  w  .ja  v  a  2  s . c o  m
        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    if (ContextCompat.checkSelfPermission(mContext,
                            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        Log.i("LocationTracker", "Permission Denied!");
                    }
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.i("LocationTracker", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                        if (latitude == 0.0 || longitude == 0.0) {//GPS is not available
                            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                    MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.i("LocationTracker", "Network Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }
            } else {
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.i("LocationTracker", "Network Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

From source file:org.wso2.carbon.iot.android.sense.event.streams.location.LocationDataReader.java

public Location getLocation() {
    try {//  w  w w.ja v a  2s .  c o  m

        locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            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();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    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();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        Log.e(TAG, "Failed to capture location data.");
    }

    return location;
}

From source file:android.support.v7.app.TwilightManager.java

private Location getLastKnownLocation() {
    Location coarseLoc = null;//from   ww  w.  j a  va2s  .co m
    Location fineLoc = null;

    int permission = PermissionChecker.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_COARSE_LOCATION);
    if (permission == PermissionChecker.PERMISSION_GRANTED) {
        coarseLoc = getLastKnownLocationForProvider(LocationManager.NETWORK_PROVIDER);
    }

    permission = PermissionChecker.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION);
    if (permission == PermissionChecker.PERMISSION_GRANTED) {
        fineLoc = getLastKnownLocationForProvider(LocationManager.GPS_PROVIDER);
    }

    if (fineLoc != null && coarseLoc != null) {
        // If we have both a fine and coarse location, use the latest
        return fineLoc.getTime() > coarseLoc.getTime() ? fineLoc : coarseLoc;
    } else {
        // Else, return the non-null one (if there is one)
        return fineLoc != null ? fineLoc : coarseLoc;
    }
}