Example usage for android.location LocationManager GPS_PROVIDER

List of usage examples for android.location LocationManager GPS_PROVIDER

Introduction

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

Prototype

String GPS_PROVIDER

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

Click Source Link

Document

Name of the GPS location provider.

Usage

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 + ")");
    }//from   w w w . j  a  va  2  s  .c  o  m
}

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 .ja v a2  s.  c  o 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.example.android.location.LocationActivity.java

private void setup() {
    Location gpsLocation = null;//  w  w  w .j  ava  2s.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.button_active);
        mBothProviderButton.setBackgroundResource(R.drawable.button_inactive);
        // 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.button_inactive);
        mBothProviderButton.setBackgroundResource(R.drawable.button_active);
        // 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.uproot.trackme.LocationActivity.java

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

    // Check if the GPS setting is currently enabled on the device.
    // This verification should be done during onStart() because the system
    // calls this method
    // when the user returns to the activity, which ensures the desired
    // location provider is
    // enabled each time the activity resumes from the stopped state.
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
        // Build an alert dialog here that requests that the user enable
        // the location services, then when the user clicks the "OK" button,
        // call enableLocationSettings()
        new EnableGpsDialogFragment().show(getSupportFragmentManager(), "enableGpsDialog");
    }/*from w  w  w.  j ava2s  .c  o m*/
    setup();
}

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();/*  w  ww.  jav  a 2  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(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.android.jhansi.designchallenge.MapFragment.java

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Location services connected.");

    Location location = null;/* w w  w  .  j a  v  a2 s  .c o m*/
    if (ActivityCompat.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            //} else {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                    MY_PERMISSION_ACCESS_FINE_LOCATION);
        }
    } else {
        LocationManager mLocationManager = (LocationManager) getActivity()
                .getSystemService(Context.LOCATION_SERVICE);

        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }

        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    }

    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    } else {
        handleNewLocation(location);
    }

}

From source file:com.example.ogadrive.HomeActivity2.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home2);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setNavigationIcon(R.drawable.home);
    //mToolbar.setLogo(R.drawable.ic_launcher);

    setSupportActionBar(mToolbar);/*from w  ww.  jav a 2  s.  co m*/

    bundle = getIntent().getExtras();
    User user = (User) bundle.getSerializable("User");

    setUser(user);
    mTitle = mDrawerTitle = getTitle();
    //      mPlanetTitles = getResources()
    //            .getStringArray(R.array.Oga_options_array);

    mPlanetTitles = new String[] {
            "" + user.getName() + System.getProperty("line.separator") + "" + user.getPhone(), "Home",
            "Book Vehicle", "History", "Emergency Contact", "Support", "About" };

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    // mDrawerLayout.setDrawerShadow(R.drawable.ic_launcher,
    // GravityCompat.START);
    // set up the drawer's list view with items and click listener
    /*mDrawerList.setAdapter(new ArrayAdapter<String>(this,
    R.layout.drawer_list_item, mPlanetTitles));*/

    mDrawerList.setAdapter(new NavigationAdapter(this, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_launcher, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(1);
    }

    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        buildAlertMessageNoGps();
    }

    Intent intentLocationSerVice = new Intent(this, com.example.services.LocationUpdateService_2.class);
    LocationUpdateService_2.token = user.getToken();
    startService(intentLocationSerVice);
}

From source file:org.hfoss.posit.android.functionplugin.reminder.SetReminder.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the transparent blank screen as a background for dialogs
    setContentView(R.layout.blank_screen);

    // Exit the Activity if the proper settings are not enabled
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean allowReminder = prefs.getBoolean("allowReminderKey", true);
    boolean allowGeoTag = prefs.getBoolean("geotagKey", true);
    // If the user allows, set the "Set Reminder" menu option
    if (!allowReminder || !allowGeoTag) {
        Toast.makeText(this, "Sorry. Geotagging and Allow Reminder settings must "
                + " be enabled to run the Set Reminder Activity.", Toast.LENGTH_LONG).show();
        finish();// w w  w.  jav a  2s . c  om
    }

    mDbEntries = getIntent().getParcelableExtra("DbEntries");

    dateSetPressed = false;

    // Get intent passed in from FindActivity
    Bundle bundle = getIntent().getExtras();
    date = bundle.getString("Date");

    // Initialize variables for Date Picker Dialog
    year = date.substring(0, 4);
    month = date.substring(5, 7);
    day = date.substring(8, 10);

    // Initialize variables for longitude and latitude      
    findsLongitude = bundle.getDouble("FindsLongitude");
    findsLatitude = bundle.getDouble("FindsLatitude");

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Location netLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Location gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location loc = null;

    if (gpsLocation != null) {
        loc = gpsLocation;
    } else {
        loc = netLocation;
    }
    if (loc != null) {
        currentLongitude = loc.getLongitude();
        currentLatitude = loc.getLatitude();
    }

    showDatePickerDialog();
}

From source file:com.sorin.cloudcog.xivelypull.LocationActivity.java

private void setup() {
    Location gpsLocation = null;// www.  j av 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.button_active);
        mBothProviderButton.setBackgroundResource(R.drawable.button_inactive);
        // 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.button_inactive);
        mBothProviderButton.setBackgroundResource(R.drawable.button_active);
        // 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.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 ww  .  j a  v a2  s . 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;
}