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:MyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);/*from  www  .j  a v a2s. co m*/
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    magFieldSensor = sensorManager.getDefaultSensor(TYPE_MAGNETIC_FIELD);
    accelerometer = sensorManager.getDefaultSensor(TYPE_ACCELEROMETER);

    sensorListener = new MySensorEventListener();

    locationListener = new MyLocationListener();
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    orientationView = (TextView) findViewById(R.id.orientationView);
    locationView = (TextView) findViewById(R.id.locationView);
}

From source file:com.entertailion.android.slideshow.images.PanoramioImageLoader.java

public PanoramioImageLoader(ImageManager sInstance, String query) {
    super(sInstance, query);
    Context context = sInstance.getContext();
    try {//from   www .j  av  a  2s .co  m
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation("static");
        mMinLong = mMaxLong = location.getLongitude();
        mMinLat = mMaxLat = location.getLatitude();
    } catch (Exception e) {
        Log.e(LOG_TAG, "LocationManager error", e);
    }
}

From source file:com.dciets.cumets.service.MyGcmListenerService.java

@Override
public void onCreate() {
    super.onCreate();
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
}

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

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

        @Override/*www .j a v a 2s. c o  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<SynchronizableElement>();
}

From source file:com.appdynamics.demo.gasp.utils.LocationServices.java

/**
 * Set Location Services and get current location
 *///  www  . ja  v a2  s. c  om
public static Location getLocation(Context context) {
    Location location = null;
    try {
        String svcName = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager) context.getSystemService(svcName);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);

        Log.i(TAG, "Current Latitude = " + location.getLatitude());
        Log.i(TAG, "Current Longitude = " + location.getLongitude());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

From source file:com.packpublishing.asynchronousandroid.chapter11.LocationActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location_layout);
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Here, thisActivity is the current activity
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION }, MY_PERMISSIONS_GPS);

    }// www .j av a  2 s. co m
    Button newSubs = (Button) findViewById(R.id.launch);
    newSubs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Runnable() {
                @Subscribe(sticky = true)
                public void onLocationEvent(LocationEvent event) {
                    Log.i(TAG, String.format("Last known Location is Lat[%f] Long[%f] ", event.latitude,
                            event.longitude));
                    TextView locationTv = (TextView) findViewById(R.id.location);
                    locationTv.setText(String.format("Lat[%f] Long[%f]", event.latitude, event.longitude));
                }

                @Override
                public void run() {
                    EventBus.getDefault().register(this);
                    EventBus.getDefault().unregister(this);
                }
            }.run();
        }
    });
}

From source file:com.ddpclient.spiovesan.ddpclient.LogService.java

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

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default/*  w  w  w .j  a v a  2 s  . c o  m*/
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);
    locationManager.requestLocationUpdates(provider, 400, 1, this);

    //Announcement about starting
    Toast.makeText(this, "Starting the Log Service: " + provider, Toast.LENGTH_SHORT).show();

    //Start a Background thread
    isRunning = true;
    Thread backgroundThread = new Thread(new BackgroundThread());
    backgroundThread.start();

    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

From source file:com.lauszus.launchpadflightcontrollerandroid.app.MapFragment.java

@Override
public void onResume() {
    super.onResume();
    LocationManager locationManager = (LocationManager) getActivity()
            .getSystemService(Context.LOCATION_SERVICE);
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        new AlertDialog.Builder(getActivity())
                .setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }//from w  w w.jav a  2  s .c o  m
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        dialog.cancel();
                        Toast.makeText(getActivity(), "GPS must be on in order to use this application!",
                                Toast.LENGTH_LONG).show();
                        getActivity().finish();
                    }
                }).create().show();
    } else {
        setUpMapIfNeeded();
        if (ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Location location = locationManager
                    .getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
            if (location != null) {
                LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 19.0f));
            }
        }
    }
}

From source file:com.sanbo.synchronizers.LocationSynchronizer.java

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

        @Override//from  www.ja  v  a2  s . c o 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.commonsware.android.weather.WeatherFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);/*from   w w  w.  jav a2s  .c om*/

    getActivity().getApplicationContext().bindService(new Intent(getActivity(), WeatherService.class), this,
            Context.BIND_AUTO_CREATE);

    mgr = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3600000, 1000, this);
}