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:ch.ethz.coss.nervousnet.vm.sensors.LocationSensor.java

@TargetApi(23)
public void startLocationCollection() {
    NNLog.d(LOG_TAG, "startLocationCollection ");

    if (locationManager == null)
        return;// w ww  . j  av a 2  s . c om
    NNLog.d(LOG_TAG, "startLocationCollection2");
    // getting GPS status
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    NNLog.d(LOG_TAG, "isGPSEnabled = " + isGPSEnabled);
    // getting network status
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    NNLog.d(LOG_TAG, "isNetworkEnabled = " + isNetworkEnabled);

    /////TODO:
    if (Build.VERSION.SDK_INT >= 23
            && ContextCompat.checkSelfPermission(mContext,
                    android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(mContext,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        setSensorState(NervousnetVMConstants.SENSOR_STATE_AVAILABLE_PERMISSION_DENIED);

    } else

    ////TODO:
    if (!isGPSEnabled && !isNetworkEnabled) {
        setSensorState(NervousnetVMConstants.SENSOR_STATE_AVAILABLE_PERMISSION_DENIED);
        NNLog.d(LOG_TAG, "Location settings disabled");
        // no network provider is enabled
        Toast.makeText(mContext, "Location settings disabled", Toast.LENGTH_LONG).show();
    } else {
        this.canGetLocation = true;
        // First get location from Network Provider
        if (isNetworkEnabled) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                    MIN_TIME_BW_UPDATES, this);
            NNLog.d(LOG_TAG, "Network");
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location != null) {
                    reading = new LocationReading(System.currentTimeMillis(),
                            new double[] { location.getLatitude(), 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_TIME_BW_UPDATES, this);

                NNLog.d(LOG_TAG, "GPS Enabled");

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        reading = new LocationReading(System.currentTimeMillis(),
                                new double[] { location.getLatitude(), location.getLongitude() });
                    }
                }
            }
        }
        dataReady(reading);
    }

}

From source file:it.unipr.informatica.autobusparma.MappaFragment.java

public Location getLocation(long MIN_DISTANCE, long MIN_TIME) {
    Location location = null;//from  w  w w  . j  a  v  a 2s .co m
    try {
        LocationManager locationManager = (LocationManager) getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
        // getting GPS status e network
        boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
                        this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,
                            this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                }
            }
        }

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

    return location;
}

From source file:com.airg.android.permission.sample.DangerousFragment.java

@SuppressWarnings("ResourceType")
private void onLocationPermissionGranted() {
    latitude.setText(R.string.loading);/*from   w  ww. ja  va 2 s. c o  m*/
    longitude.setText(R.string.loading);

    final Activity activity = getActivity();
    gps = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    updateLocation(gps.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
    gps.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
}

From source file:com.example.isse.weatherapp.ui.WeatherListActivity.java

public void startLocationDetection() {
    this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (checkPermission()) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_UPDATES, DISTANCE_UPDATES,
                this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_UPDATES, DISTANCE_UPDATES,
                this);
        Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location location = gpsLocation != null ? gpsLocation : networkLocation;
        Log.v("create", "location(" + location + ")");
    }/*  w w w. ja  v a 2 s . c  om*/
}

From source file:semanticweb.hws14.movapp.activities.Criteria.java

public void getGpsLocation() {
    setProgressBarIndeterminateVisibility(true);
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListner);
}

From source file:com.example.get_location.Get_location.java

private void setup() {
    Location gpsLocation = null;//  w  ww .  jav  a  2  s . c o  m
    Location networkLocation = null;
    mLocationManager.removeUpdates(listener);
    mLatLng.setText(R.string.unknown);
    mAddress.setText(R.string.unknown);
    // Get fine location updates only.
    if (mUseFine) {
        mFineProviderButton.setBackgroundResource(R.drawable.ic_launcher);
        mBothProviderButton.setBackgroundResource(R.drawable.ic_launcher);
        // Request updates from just the fine (gps) provider.
        gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
        // Update the UI immediately if a location is obtained.
        if (gpsLocation != null)
            updateUILocation(gpsLocation);
    } else if (mUseBoth) {
        // Get coarse and fine location updates.
        mFineProviderButton.setBackgroundResource(R.drawable.ic_launcher);
        mBothProviderButton.setBackgroundResource(R.drawable.ic_launcher);
        // Request updates from both fine (gps) and coarse (network) providers.
        gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
        networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER,
                R.string.not_support_network);

        // If both providers return last known locations, compare the two and use the better
        // one to update the UI.  If only one provider returns a location, use it.
        if (gpsLocation != null && networkLocation != null) {
            updateUILocation(getBetterLocation(gpsLocation, networkLocation));
        } else if (gpsLocation != null) {
            updateUILocation(gpsLocation);
        } else if (networkLocation != null) {
            updateUILocation(networkLocation);
        }
    }
}

From source file:com.nextgis.mobile.TrackerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d(MainActivity.TAG, "Received start id " + startId + ": " + intent);

    super.onStartCommand(intent, flags, startId);

    if (intent == null)
        return START_STICKY;

    String action = intent.getAction();
    if (action == null)
        return START_STICKY;

    Log.d(MainActivity.TAG, "action " + action);
    if (action.equals(ACTION_STOP)) {
        trackerLocationListener.setWritePostion(false);
        if (dbHelper != null)
            dbHelper.close();//from w  w w  .j a v  a2  s.com
        if (!trackerLocationListener.isWriteTrack())
            stopSelf();
    } else if (action.equals(ACTION_STOP_GPX)) {
        m_TrakAddPointHandler = null;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(mNotifyId);

        trackerLocationListener.StoreTrack(false);
        trackerLocationListener.setWriteTrack(false);
        if (!trackerLocationListener.isWritePostion())
            stopSelf();
    } else if (action.equals(ACTION_START)) {
        SharedPreferences prefs = getSharedPreferences(PreferencesActivity.SERVICE_PREF,
                MODE_PRIVATE | MODE_MULTI_PROCESS);
        boolean isStrarted = prefs.getBoolean(PreferencesActivity.KEY_PREF_SW_TRACK_SRV, false);

        if (isStrarted) {
            if (!trackerLocationListener.isWritePostion()) {
                trackerLocationListener.setWritePostion(true);

                dbHelper = new PositionDatabase(getApplicationContext());
                PositionDB = dbHelper.getWritableDatabase();

                long nMinDistChangeForUpdates = prefs
                        .getLong(PreferencesActivity.KEY_PREF_MIN_DIST_CHNG_UPD + "_long", 25);
                long nMinTimeBetweenUpdates = prefs.getLong(PreferencesActivity.KEY_PREF_MIN_TIME_UPD + "_long",
                        0);

                Log.d(MainActivity.TAG, "start LocationManager MinDist:" + nMinDistChangeForUpdates
                        + " MinTime:" + nMinTimeBetweenUpdates);

                Log.d(MainActivity.TAG, "start LocationManager.GPS_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);
                Log.d(MainActivity.TAG, "start LocationManager.NETWORK_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);
                Log.d(MainActivity.TAG, "request end");

            }
            boolean bEnergyEconomy = prefs.getBoolean(PreferencesActivity.KEY_PREF_SW_ENERGY_ECO, true);
            long nMinTimeBetweenSend = prefs.getLong(PreferencesActivity.KEY_PREF_TIME_DATASEND + "_long",
                    DateUtils.MINUTE_IN_MILLIS);
            ScheduleNextUpdate(this.getApplicationContext(), TrackerService.ACTION_START, nMinTimeBetweenSend,
                    bEnergyEconomy, isStrarted);
        }
    } else if (action.equals(ACTION_START_GPX)) {
        SharedPreferences prefs = getSharedPreferences(PreferencesActivity.SERVICE_PREF,
                MODE_PRIVATE | MODE_MULTI_PROCESS);
        boolean isStrarted_GPX = prefs.getBoolean(PreferencesActivity.KEY_PREF_SW_TRACKGPX_SRV, false);

        if (isStrarted_GPX) {
            if (!trackerLocationListener.isWriteTrack()) {
                trackerLocationListener.setWriteTrack(true);

                long nMinDistChangeForUpdates = prefs
                        .getLong(PreferencesActivity.KEY_PREF_MIN_DIST_CHNG_UPD + "_long", 25);
                long nMinTimeBetweenUpdates = prefs.getLong(PreferencesActivity.KEY_PREF_MIN_TIME_UPD + "_long",
                        0);

                Log.d(MainActivity.TAG, "start GPX LocationManager MinDist:" + nMinDistChangeForUpdates
                        + " MinTime:" + nMinTimeBetweenUpdates);

                Log.d(MainActivity.TAG, "start GPX LocationManager.GPS_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);

                Intent resultIntent = new Intent(this, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this).addParentStack(MainActivity.class)
                        .addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.record_start_notify)
                        .setContentTitle(getString(R.string.app_name)).setOngoing(true)
                        .setContentText(getString(R.string.gpx_recording))
                        .setContentIntent(resultPendingIntent);

                Notification noti = mBuilder.getNotification();
                //noti.flags |= Notification.FLAG_FOREGROUND_SERVICE;//Notification.FLAG_NO_CLEAR | 
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(mNotifyId, noti);

            }
        }
        boolean bEnergyEconomy = prefs.getBoolean(PreferencesActivity.KEY_PREF_SW_ENERGY_ECO, true);
        long nMinTimeBetweenSend = prefs.getLong(PreferencesActivity.KEY_PREF_TIME_DATASEND + "_long",
                DateUtils.MINUTE_IN_MILLIS);
        ScheduleNextUpdate(getApplicationContext(), TrackerService.ACTION_START_GPX, nMinTimeBetweenSend,
                bEnergyEconomy, isStrarted_GPX);
    }
    return START_STICKY;
}

From source file:com.androzic.location.LocationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null || intent.getAction() == null)
        return 0;

    if (intent.getAction().equals(ENABLE_LOCATIONS) && !locationsEnabled) {
        locationsEnabled = true;/*  w  w  w .j  a v  a  2s  .c  o m*/
        connect();
        sendBroadcast(new Intent(BROADCAST_LOCATING_STATUS));
        if (trackingEnabled) {
            sendBroadcast(new Intent(BROADCAST_TRACKING_STATUS));
        }
    }
    if (intent.getAction().equals(DISABLE_LOCATIONS) && locationsEnabled) {
        locationsEnabled = false;
        disconnect();
        updateProvider(LocationManager.GPS_PROVIDER, false);
        updateProvider(LocationManager.NETWORK_PROVIDER, false);
        sendBroadcast(new Intent(BROADCAST_LOCATING_STATUS));
        if (trackingEnabled) {
            closeDatabase();
            sendBroadcast(new Intent(BROADCAST_TRACKING_STATUS));
        }
    }
    if (intent.getAction().equals(ENABLE_TRACK) && !trackingEnabled) {
        errorMsg = "";
        errorTime = 0;
        trackingEnabled = true;
        isContinous = false;
        openDatabase();
        sendBroadcast(new Intent(BROADCAST_TRACKING_STATUS));
    }
    if (intent.getAction().equals(DISABLE_TRACK) && trackingEnabled) {
        trackingEnabled = false;
        closeDatabase();
        errorMsg = "";
        errorTime = 0;
        sendBroadcast(new Intent(BROADCAST_TRACKING_STATUS));
    }
    updateNotification();

    return START_REDELIVER_INTENT | START_STICKY;
}

From source file:com.nextgis.mobile.services.TrackerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d(TAG, "Received start id " + startId + ": " + intent);

    super.onStartCommand(intent, flags, startId);

    if (intent == null)
        return START_STICKY;

    String action = intent.getAction();
    if (action == null)
        return START_STICKY;

    Log.d(TAG, "action " + action);
    if (action.equals(ACTION_STOP)) {
        trackerLocationListener.setWritePostion(false);
        if (dbHelper != null)
            dbHelper.close();/*from  ww w.jav  a  2 s .  co  m*/
        if (!trackerLocationListener.isWriteTrack())
            stopSelf();
    } else if (action.equals(ACTION_STOP_GPX)) {
        m_TrakAddPointHandler = null;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(mNotifyId);

        trackerLocationListener.StoreTrack(false);
        trackerLocationListener.setWriteTrack(false);
        if (!trackerLocationListener.isWritePostion())
            stopSelf();
    } else if (action.equals(ACTION_START)) {
        SharedPreferences prefs = getSharedPreferences(Constants.SERVICE_PREF,
                MODE_PRIVATE | MODE_MULTI_PROCESS);
        boolean isStrarted = prefs.getBoolean(Constants.KEY_PREF_SW_TRACK_SRV, false);

        if (isStrarted) {
            if (!trackerLocationListener.isWritePostion()) {
                trackerLocationListener.setWritePostion(true);

                dbHelper = new PositionDatabase(getApplicationContext());
                PositionDB = dbHelper.getWritableDatabase();

                long nMinDistChangeForUpdates = prefs.getLong(Constants.KEY_PREF_MIN_DIST_CHNG_UPD + "_long",
                        25);
                long nMinTimeBetweenUpdates = prefs.getLong(Constants.KEY_PREF_MIN_TIME_UPD + "_long", 0);

                Log.d(TAG, "start LocationManager MinDist:" + nMinDistChangeForUpdates + " MinTime:"
                        + nMinTimeBetweenUpdates);

                Log.d(TAG, "start LocationManager.GPS_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);
                Log.d(TAG, "start LocationManager.NETWORK_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);
                Log.d(TAG, "request end");

            }
            boolean bEnergyEconomy = prefs.getBoolean(Constants.KEY_PREF_SW_ENERGY_ECO, true);
            long nMinTimeBetweenSend = prefs.getLong(Constants.KEY_PREF_TIME_DATASEND + "_long",
                    DateUtils.MINUTE_IN_MILLIS);
            ScheduleNextUpdate(this.getApplicationContext(), TrackerService.ACTION_START, nMinTimeBetweenSend,
                    bEnergyEconomy, isStrarted);
        }
    } else if (action.equals(ACTION_START_GPX)) {
        SharedPreferences prefs = getSharedPreferences(Constants.SERVICE_PREF,
                MODE_PRIVATE | MODE_MULTI_PROCESS);
        boolean isStrarted_GPX = prefs.getBoolean(Constants.KEY_PREF_SW_TRACKGPX_SRV, false);

        if (isStrarted_GPX) {
            if (!trackerLocationListener.isWriteTrack()) {
                trackerLocationListener.setWriteTrack(true);

                long nMinDistChangeForUpdates = prefs.getLong(Constants.KEY_PREF_MIN_DIST_CHNG_UPD + "_long",
                        25);
                long nMinTimeBetweenUpdates = prefs.getLong(Constants.KEY_PREF_MIN_TIME_UPD + "_long", 0);

                Log.d(TAG, "start GPX LocationManager MinDist:" + nMinDistChangeForUpdates + " MinTime:"
                        + nMinTimeBetweenUpdates);

                Log.d(TAG, "start GPX LocationManager.GPS_PROVIDER");
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, nMinTimeBetweenUpdates,
                        nMinDistChangeForUpdates, trackerLocationListener);

                Intent resultIntent = new Intent(this, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this).addParentStack(MainActivity.class)
                        .addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.record_start_notify)
                        .setContentTitle(getString(R.string.app_name)).setOngoing(true)
                        .setContentText(getString(R.string.gpx_recording))
                        .setContentIntent(resultPendingIntent);

                Notification noti = mBuilder.getNotification();
                //noti.flags |= Notification.FLAG_FOREGROUND_SERVICE;//Notification.FLAG_NO_CLEAR | 
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(mNotifyId, noti);

            }
        }
        boolean bEnergyEconomy = prefs.getBoolean(Constants.KEY_PREF_SW_ENERGY_ECO, true);
        long nMinTimeBetweenSend = prefs.getLong(Constants.KEY_PREF_TIME_DATASEND + "_long",
                DateUtils.MINUTE_IN_MILLIS);
        ScheduleNextUpdate(getApplicationContext(), TrackerService.ACTION_START_GPX, nMinTimeBetweenSend,
                bEnergyEconomy, isStrarted_GPX);
    }
    return START_STICKY;
}

From source file:com.adampmarshall.speedo.LocationActivity.java

private void setup() {
    Location gpsLocation = null;//from  w  w  w  . j  a  v a  2  s  .  c  o m
    Location networkLocation = null;
    mLocationManager.removeUpdates(listener);
    mSpeed.setText(R.string.unknown);
    // Get fine location updates only.
    if (mUseFine) {
        // Request updates from just the fine (gps) provider.
        gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
        // Update the UI immediately if a location is obtained.
        if (gpsLocation != null)
            updateUILocation(gpsLocation);
    } else if (mUseBoth) {
        // Request updates from both fine (gps) and coarse (network)
        // providers.
        gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
        networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER,
                R.string.not_support_network);

        // If both providers return last known locations, compare the two
        // and use the better
        // one to update the UI. If only one provider returns a location,
        // use it.
        if (gpsLocation != null && networkLocation != null) {
            updateUILocation(getBetterLocation(gpsLocation, networkLocation));
        } else if (gpsLocation != null) {
            updateUILocation(gpsLocation);
        } else if (networkLocation != null) {
            updateUILocation(networkLocation);
        }
    }

}