Example usage for android.location LocationManager requestLocationUpdates

List of usage examples for android.location LocationManager requestLocationUpdates

Introduction

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

Prototype

@UnsupportedAppUsage
    private void requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper,
            PendingIntent intent) 

Source Link

Usage

From source file:Main.java

public static void beginListening(Context ctx, LocationListener ll) {
    LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    if (lm.getProvider(LocationManager.GPS_PROVIDER) != null) {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    } else if (lm.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
    } else {//from   www.  ja va  2  s  . com
        lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, ll);
    }
}

From source file:Main.java

/**
 * Method to register location updates with a desired location provider.  If the requested
 * provider is not available on the device, the app displays a Toast with a message referenced
 * by a resource id.//from ww w  . j  av a  2 s .c om
 *
 * @param provider Name of the requested provider.
 * @param errorResId Resource id for the string message to be displayed if the provider does
 *                   not exist on the device.
 * @return A previously returned {@link android.location.Location} from the requested provider,
 *         if exists.
 */
public static Location requestUpdatesFromProvider(LocationManager locationManager,
        LocationListener locationListener, final String provider, final int errorResId) {
    Location location = null;

    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider, RUN_ONCE, RUN_ONCE, locationListener);
        location = locationManager.getLastKnownLocation(provider);
    }

    return location;
}

From source file:it.feio.android.omninotes.utils.GeocodeHelper.java

public static LocationManager getLocationManager(Context context, final LocationListener locationListener) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // A check is done to avoid crash when NETWORK_PROVIDER is not
    // available (ex. on emulator with API >= 11)
    if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)
            && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 50, locationListener);
    }//from  w  w w  .  java2 s . co m
    if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)
            && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 50, locationListener);
    } else {
        locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 60000, 50, locationListener);
    }
    return locationManager;
}

From source file:com.airbop.library.simple.CommonUtilities.java

/** 
 * Get the current location from the location manager, and when we get it
 * post that information to the Airbop servers
 * @param appContext//from   w  ww.  j a  va 2s  .c  o m
 * @param regId
 * @return
 */
public static boolean getCurrentLocation(LocationListener locationListener, final Context appContext) {
    if (true) {
        Criteria criteria = getCriteria();
        LocationManager locationManager = (LocationManager) appContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
                // We've posted so let the caller know
                return true;
            }
        }
    }
    // We couldn't get the location manager so let the caller know
    return false;
}

From source file:com.binomed.showtime.android.util.localisation.LocationUtils.java

public static void registerLocalisationListener(final Context context, final ProviderEnum provider,
        final LocalisationManagement listener) {
    switch (provider) {
    case GPS_PROVIDER:
    case GSM_PROVIDER: {
        if (isLocalisationEnabled(context, provider)) {
            LocationManager locationManager = getLocationManager(context);
            if (locationManager != null) {
                locationManager.requestLocationUpdates(provider.getAndroidProvider(), 10000, 10, listener);
            } else {
                if (Log.isLoggable(TAG, Log.DEBUG)) {
                    Log.d(TAG, "No listener Put"); //$NON-NLS-1$
                    listener.onProviderDisabled(null);
                }//from   w w  w  .j a v  a 2  s  . c om
            }
        } else {
            listener.onProviderDisabled(null);
        }
        break;
    }
    case IP_PROVIDER:
    case WIFI_PROVIDER:
    case XPS_PROVIDER: {
        final XPS xps = listener.getXps();
        final WPSAuthentication auth = listener.getWpsAuth();
        xps.registerUser(auth, null, new RegistrationCallback() {

            @Override
            public WPSContinuation handleError(WPSReturnCode error) {
                switch (error) {
                case WPS_ERROR_LOCATION_CANNOT_BE_DETERMINED: {
                    Log.e(TAG, error.toString());
                    break;
                }
                case WPS_ERROR_WIFI_NOT_AVAILABLE: {
                    Log.e(TAG, error.toString());
                    break;
                }
                case WPS_ERROR_SERVER_UNAVAILABLE: {
                    Log.e(TAG, error.toString());
                    break;
                }
                case WPS_ERROR_NO_WIFI_IN_RANGE: {
                    Log.e(TAG, error.toString());

                    break;
                }
                case WPS_ERROR: {
                    Log.e(TAG, error.name());

                    break;
                }
                default:
                    Log.e(TAG, error.name());
                    break;
                }
                // TODO grer les cas d'erreur
                // in all case, we'll stop
                return WPSContinuation.WPS_STOP;
            }

            @Override
            public void done() {
                Log.i(TAG, "Registration Done ! ");

            }

            @Override
            public void handleSuccess() {

                switch (provider) {
                case IP_PROVIDER: {
                    xps.getIPLocation(auth //
                    , WPSStreetAddressLookup.WPS_LIMITED_STREET_ADDRESS_LOOKUP //
                    , listener//
                    );
                    break;
                }
                case WIFI_PROVIDER: {
                    xps.getLocation(auth //
                    , WPSStreetAddressLookup.WPS_LIMITED_STREET_ADDRESS_LOOKUP //
                    , listener//
                    );
                    break;
                }
                case XPS_PROVIDER: {
                    xps.getXPSLocation(auth //
                    , (5000 / 1000) //
                    , 30 //
                    , listener//
                    );

                }
                default:
                    break;
                }
            }
        });
        break;
    }
    default:
        break;
    }
}

From source file:com.dotd.mgrs.gps.MGRSLocationListener.java

public int onStartCommand(Intent intent, int flags, int startId) {
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    sendMessageBroadcast(getString(R.string.acquiring));

    return Service.START_NOT_STICKY;
}

From source file:in.ac.iitb.cse.cartsbusboarding.ui.MainActivity.java

private void appendGpsData() {
    LocationManager gpsMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    GsmListener gpsListener = new GsmListener();
    gpsMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
    final GsmData gpsData = gpsListener.getCurrentData();
    if (gpsData != null && gpsData.getLocation() != null) {
        TextView twData = (TextView) findViewById(R.id.section_data_gsm);
        twData.setText(twData.getText() + "GPS Speed:" + gpsData.getLocation().getSpeed());
    }//  ww w .j a v a 2  s .  c om
}

From source file:org.runnerup.tracker.GpsStatus.java

public void start(TickListener listener) {
    clear(true);/*from w  w w  .j a v  a  2s .co  m*/
    this.listener = listener;
    if (ContextCompat.checkSelfPermission(this.context,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        try {
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        } catch (Exception ex) {
            lm = null;
        }
        if (lm != null) {
            locationManager = lm;
            locationManager.addGpsStatusListener(this);
        }
    }
}

From source file:org.mozilla.mozstumbler.service.stumblerthread.scanners.GPSScanner.java

private void startActiveMode() {
    LocationManager lm = getLocationManager();
    if (!isGpsAvailable(lm)) {
        return;/*from www .  j ava  2s .com*/
    }

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, ACTIVE_MODE_GPS_MIN_UPDATE_TIME_MS,
            ACTIVE_MODE_GPS_MIN_UPDATE_DISTANCE_M, this);

    reportLocationLost();

    mGPSListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                GpsStatus status = getLocationManager().getGpsStatus(null);
                Iterable<GpsSatellite> sats = status.getSatellites();

                int satellites = 0;
                int fixes = 0;

                for (GpsSatellite sat : sats) {
                    satellites++;
                    if (sat.usedInFix()) {
                        fixes++;
                    }
                }

                if (fixes < MIN_SAT_USED_IN_FIX) {
                    reportLocationLost();
                }
            } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
                reportLocationLost();
            }
        }
    };

    lm.addGpsStatusListener(mGPSListener);
}

From source file:com.example.android.touroflondon.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    //Verify that Google Play Services is available
    int playStatus = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (playStatus != ConnectionResult.SUCCESS) {
        // Google Play services is not available, prompt user and close application when dialog is dismissed
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(playStatus, this, 0);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override/*from  w  w  w.j  av a 2 s.  c o m*/
            public void onCancel(DialogInterface dialog) {
                finish();
            }
        });
        dialog.show();

        // Hide all active fragments
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.hide(mMapFragment);
        if (mPoiListFragment != null) {
            ft.hide(mPoiListFragment);
        }
        ft.commit();

    } else {

        // Getting reference to the SupportMapFragment of activity_main.xml
        TourMapFragment fm = (TourMapFragment) getFragmentManager().findFragmentById(R.id.fragment_map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, mMapFragment);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20000, 0, mMapFragment);
        //            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        // Make sure active fragments are shown when returning from Play Services dialog interaction
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.show(mMapFragment);
        if (mPoiListFragment != null) {
            ft.show(mPoiListFragment);
        }
        ft.commit();

    }

}