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.appsimobile.appsii.LocationLoader.java

@RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION })
public boolean requestLocationUpdate(Context context) throws SecurityException {

    if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        return false;
    }// www.j a  v  a 2 s  .c o  m

    Location lastKnown = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (lastKnown != null) {
        onLocationChanged(lastKnown);
    }
    mLocationListener = new LocationListenerImpl(this);
    mLocationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, mLocationListener, null);
    return true;
}

From source file:ca.ualberta.cs.drivr.GPSTracker.java

/**
 * Gets the User Current Location through GPS or Wifi
 * @return returns user current Location.
 *///from  w  ww  . j  ava 2s.co  m
public Location getLocation() {
    try {
        locationManger = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        // Check that App can get Location
        isGPSEnabled = locationManger.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManger.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // Services not Enabled
            enableGPS();
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                location = null;
                if (ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(mContext,
                                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
                    if (locationManger != null) {
                        location = locationManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (location != null) {
                            myLatitude = location.getLatitude();
                            myLongitude = location.getLongitude();
                        }
                    }

                }
            }
            if (isGPSEnabled) {
                location = null;
                if (ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(mContext,
                                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
                    if (locationManger != null) {
                        location = locationManger.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (location != null) {
                            myLatitude = location.getLatitude();
                            myLongitude = location.getLongitude();
                        }
                    }

                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (myLocation != null) {
        return myLocation;
    } else {
        // Android Emulator Can't Use Wifi
        myLocation = new Location("Edmonton");
        myLocation.setLatitude(53.5232);
        myLocation.setLongitude(-113.5263);
        myLatitude = myLocation.getLatitude();
        myLongitude = myLocation.getLongitude();

        return myLocation;
    }
}

From source file:com.poguico.palmabici.synchronizers.LocationSynchronizer.java

public LocationSynchronizer(FragmentActivity context) {
    manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    listener = new LocationListener() {

        @Override//from   w w w .  j a va2 s .c  om
        public void onLocationChanged(Location l) {
            location = l;
            updateViews();
        }

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

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
            if ((provider.equals(LocationManager.GPS_PROVIDER)
                    && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
                    || (provider.equals(LocationManager.NETWORK_PROVIDER)
                            && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER))) {
                location = null;
                updateViews();
            }
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000L, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000L, 0, listener);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    synchronizableElements = new ArrayList<SynchronizableElement>();
}

From source file:org.apache.cordova.geolocation.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 ww w.  j ava  2s  .  c  om*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (locationManager == null) {
        locationManager = (LocationManager) this.cordova.getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
    }
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        if (networkListener == null) {
            networkListener = new NetworkListener(locationManager, this);
        }
        if (gpsListener == null) {
            gpsListener = new GPSListener(locationManager, this);
        }

        if (action.equals("getLocation")) {
            boolean enableHighAccuracy = args.getBoolean(0);
            int maximumAge = args.getInt(1);
            String provider = (enableHighAccuracy
                    && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                            ? LocationManager.GPS_PROVIDER
                            : LocationManager.NETWORK_PROVIDER;
            Location last = this.locationManager.getLastKnownLocation(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:com.sanbo.synchronizers.LocationSynchronizer.java

public LocationSynchronizer(FragmentActivity context) {
    manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    listener = new LocationListener() {

        @Override//from w  ww  .  jav a 2 s . co m
        public void onLocationChanged(Location l) {
            location = l;
            updateViews();
        }

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

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
            if ((provider.equals(LocationManager.GPS_PROVIDER)
                    && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
                    || (provider.equals(LocationManager.NETWORK_PROVIDER)
                            && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER))) {
                location = null;
                updateViews();
            }
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000L, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000L, 0, listener);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null)
        location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    synchronizableElements = new ArrayList<SynchronizableActivity>();
}

From source file:com.nextgis.maplibui.util.NotificationHelper.java

public static void showLocationInfo(final Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (!preferences.getBoolean(SettingsConstantsUI.KEY_PREF_SHOW_GEO_DIALOG, true))
        return;/*  ww  w.  ja v a  2  s.c om*/

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    final boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    final boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!isGPSEnabled || !isNetworkEnabled) {
        String title, info;

        if (!isGPSEnabled && !isNetworkEnabled) {
            title = context.getString(R.string.location_disabled);
            info = context.getString(R.string.location_disabled_msg);
        } else {
            String network = "", gps = "";

            if (!isNetworkEnabled)
                network = "\r\n- " + context.getString(R.string.location_network);

            if (!isGPSEnabled)
                gps = "\r\n- " + context.getString(R.string.location_gps);

            title = context.getString(R.string.location_accuracy);
            info = context.getString(R.string.location_inaccuracy) + network + gps;
        }

        if (context instanceof Activity)
            showLocationDialog(context, title, info);
        else
            showLocationNotification(context, title, info);
    }
}

From source file:org.apache.cordova.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 ww  w  . ja  va2s.  com*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (locationManager == null) {
        locationManager = (LocationManager) this.cordova.getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
    }

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        if (networkListener == null) {
            networkListener = new NetworkListener(locationManager, this);
        }
        if (gpsListener == null) {
            gpsListener = new GPSListener(locationManager, this);
        }

        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:me.ziccard.secureit.async.upload.PeriodicPositionUploaderTask.java

/**
 * Constructor/*ww  w . j a v  a2  s.co m*/
 * @param context
 * @param lat
 * @param lng
 */
public PeriodicPositionUploaderTask(Context context) {
    this.context = context;
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    prefs = new SecureItPreferences(context);

    this.phoneId = prefs.getPhoneId();
    this.accessToken = prefs.getAccessToken();
}

From source file:com.orange.datavenue.LocationService.java

public void start() {
    Log.d(TAG_NAME, "start()");
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mGpsListener = new MyLocationListener();
    mNetworkListener = new MyLocationListener();

    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, mGpsListener);
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, mNetworkListener);
}

From source file:com.digzdigital.eservicedriver.MixedPositionProvider.java

private void startBackupProvider() {
    Log.i(TAG, "backup provider start");
    if (backupListener == null) {

        backupListener = new LocationListener() {
            @Override//w  ww  .  j a va 2  s  .c  om
            public void onLocationChanged(Location location) {
                Log.i(TAG, "backup provider location");
                updateLocation(location);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, period, 0, backupListener);
    }
}