Example usage for android.content Context LOCATION_SERVICE

List of usage examples for android.content Context LOCATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context LOCATION_SERVICE.

Prototype

String LOCATION_SERVICE

To view the source code for android.content Context LOCATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.location.LocationManager for controlling location updates.

Usage

From source file:com.findme.adapter.ImageListAdapter.java

public ImageListAdapter(JSONArray list, LayoutInflater inflator, ImageListActivity mainAct, String prefix) {
    super();/* w  ww  .j av  a 2s.c o  m*/
    this.inflator = inflator;
    this.mainAct = mainAct;
    dataList = list;
    this.prefix = prefix;
    this.http = AndroidHttpClient.newInstance("AndroidApp");
    locationManager = (LocationManager) mainAct.getSystemService(Context.LOCATION_SERVICE);
}

From source file:com.example.scandevice.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();/*from   w  w  w .  j ava2s. c o m*/
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    // Getting the name of the provider that meets the criteria
    provider = locationManager.getBestProvider(criteria, true);

    if (provider == null && !locationManager.isProviderEnabled(provider)) {

        // Get the location from the given provider
        List<String> list = locationManager.getAllProviders();

        for (int i = 0; i < list.size(); i++) {
            //Get device name;
            String temp = list.get(i);

            //check usable
            if (locationManager.isProviderEnabled(temp)) {
                provider = temp;
                break;
            }
        }
    }
    //get location where reference last.
    Location location = locationManager.getLastKnownLocation(provider);

    if (location == null)
        Toast.makeText(this, "There are no available position information providers.", Toast.LENGTH_SHORT)
                .show();
    else
        //GPS start from last location.
        onLocationChanged(location);

    // Set by <content src="index.html" /> in config.xml
    loadUrl(launchUrl);
}

From source file:com.dwdesign.tweetings.activity.MapViewerActivity.java

protected void getLocationAndCenterMap() {
    LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    final String provider = mLocationManager.getBestProvider(criteria, true);

    Location mRecentLocation = null;

    if (provider != null) {
        mRecentLocation = mLocationManager.getLastKnownLocation(provider);
    } else {//from   w  ww.jav a 2s  . c o  m
        Toast.makeText(this, R.string.cannot_get_location, Toast.LENGTH_SHORT).show();
    }

    if (mRecentLocation != null && isNativeMapSupported()) {
        NativeMapFragment aFragment = (NativeMapFragment) mFragment;
        aFragment.setCenter(mRecentLocation);
    }
}

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 w w. j  a va 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:de.grobox.liberario.locations.LocationGpsView.java

public LocationGpsView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (!isInEditMode()) {
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }//  w w  w  .j  av a2s.  c  om
}

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;//w w  w  .  j a v  a2  s  . c  o  m

    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:com.nuig.trafficapp.MyGcmListenerService.java

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences settings = getSharedPreferences("TrafficApp", 0);
    accountName = settings.getString("accountName", null);

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}

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:de.grobox.liberario.ui.LocationGpsView.java

public LocationGpsView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (!isInEditMode()) {
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }/*from  ww w .  j  a v  a2s  .co  m*/

    getAdapter().setGPS(true);
}

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

/**
 * Constructor./*from w ww .j av a  2s .  com*/
 *
 * @param applicationContext the application context.
 * @param activity the MainActivity.
 */
public MobileLocationManager(Context applicationContext, Activity activity) {
    this.applicationContext = applicationContext;
    this.activity = activity;
    locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
}