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.example.aximcore.hangyamaps.MapsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();//from   ww w . j  a v  a 2  s. c  o m

    // Begin ****************************************
    if (mMap != null) {
        mMap.setMyLocationEnabled(true); // sajt lokalizci engedlyezse
    }

    latituteField = (TextView) findViewById(R.id.TextView01); // szllesgi
    longitudeField = (TextView) findViewById(R.id.TextView02); // hosszsgi
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // rendszerszolgltatsok lekrse
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false); // legjobb szolgltats lekrse ( jelen esetben GPS )
    Location location = locationManager.getLastKnownLocation(provider); // utols ismert helyzetnk

    if (location != null) {
        System.out.println("Provider " + provider + " kivlasztva.");
        onLocationChanged(location); // ha helyzetnk megvltozna
    } else {
        latituteField.setText("Hely nem elrhet!");
        longitudeField.setText("Hely nem elrhet");
    }
    // End ******************************************
}

From source file:com.memetro.android.alerts.MapFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (inflaterMap != null) {
        ViewGroup parent = (ViewGroup) inflaterMap.getParent();
        if (parent != null)
            parent.removeView(inflaterMap);
    }//from ww  w.java 2s  .c om
    try {
        inflaterMap = inflater.inflate(R.layout.fragment_map, container, false);
    } catch (InflateException e) {
        //
    }

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_fragment)).getMap();

    // TODO Esto hay que revisarlo.
    if (map == null) {
        Crashlytics.log(1, TAG, "Map is null > " + map + " inflaterMap > " + inflaterMap);
        return inflaterMap;
    }

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    followMeLocationSource = new FollowMeLocationSource();

    map.clear();
    map.setLocationSource(followMeLocationSource);
    map.getUiSettings().setZoomControlsEnabled(true);
    map.getUiSettings().setZoomGesturesEnabled(true);
    map.getUiSettings().setCompassEnabled(true);
    map.setMyLocationEnabled(true);

    List<Alert> alerts = DataUtils.getAlerts();

    MarkerOptions markerOptions = new MarkerOptions();

    for (Alert alert : alerts) {
        latlng = new LatLng(alert.latitude, alert.longitude);

        markerOptions.icon(BitmapDescriptorFactory.fromResource(getMarkerResource(alert.date, alert.icon)));
        map.addMarker(markerOptions.position(latlng));
    }
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 8));

    return inflaterMap;
}

From source file:eu.inmite.apps.smsjizdenka.fragment.CitiesFragment.java

@Override
public Loader<Object> onCreateLoader(int id, Bundle arg1) {
    if (id == Constants.LOADER_CITIES) {
        return new WrappedAsyncTaskLoader<Object>(c) {

            @Override/*from   w w  w.  jav  a2 s .  com*/
            public Object loadInBackground() {
                List<CitiesAdapter.Item> items = new ArrayList<CitiesAdapter.Item>();
                CityManager cm = CityManager.get(c);
                // one closest
                LocationManager lm = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
                Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location != null) {
                    City closest = cm.getClosest(c, location.getLatitude(), location.getLongitude());
                    if (closest != null) {
                        items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_HEADER,
                                c.getString(R.string.cities_nearest)));
                        items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_CITY, closest.city));
                    }
                }
                // two last
                List<City> lastTwo = cm.getLastTwoUsed(c);
                if (lastTwo.size() > 0) {
                    items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_HEADER,
                            c.getString(R.string.cities_recently_purchased)));
                    items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_CITY, lastTwo.get(0).city));
                    if (lastTwo.size() == 2) {
                        items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_CITY, lastTwo.get(1).city));
                    }
                }
                // all alphabetically
                List<City> all = cm.getUniqueCities(c);
                items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_HEADER,
                        c.getString(R.string.cities_all_tickets)));
                for (City city : all) {
                    items.add(new CitiesAdapter.Item(CitiesAdapter.Item.TYPE_CITY, city.city));
                }
                return items;
            }
        };
    }
    return null;
}

From source file:com.artur.softwareproject.GpsService.java

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "GpsService was created.");

    gpsStatus = false;//ww w  .ja va2  s . c  om

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

    checkPermission("android.permission.ACCESS_FINE_LOCATION", 1, 0);

    gpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    Thread gpsStatusThread = new Thread(this);
    gpsStatusThread.start();
}

From source file:com.handlerexploit.news.fragments.WeatherFragment.java

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

    final Activity activity = getActivity();
    if (activity != null) {
        threadPool.execute(new Runnable() {
            public void run() {
                final SharedPreferences sharedPreferences = activity.getSharedPreferences(".preferences",
                        Context.MODE_PRIVATE);
                String location = sharedPreferences.getString("Location", null);
                if (location != null) {
                    final WeatherInfo weatherInfo = WeatherProvider.getWeather(location);
                    handler.post(new Runnable() {
                        public void run() {
                            render(weatherInfo);
                        }//  ww  w . j  av  a  2  s  . com
                    });
                } else {
                    Location lastKnownLocation = ((LocationManager) activity
                            .getSystemService(Context.LOCATION_SERVICE))
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (lastKnownLocation != null) {
                        double longitude = lastKnownLocation.getLongitude();
                        double latitude = lastKnownLocation.getLatitude();

                        String url = "http://maps.google.com/maps/geo?ll=" + latitude + "," + longitude;
                        try {
                            String response = EntityUtils
                                    .toString(new DefaultHttpClient().execute(new HttpGet(url)).getEntity());
                            JSONObject addressDetails = new JSONObject(response).getJSONArray("Placemark")
                                    .getJSONObject(0).getJSONObject("AddressDetails");
                            JSONObject locality = addressDetails.getJSONObject("Country")
                                    .getJSONObject("AdministrativeArea").getJSONObject("Locality");

                            String raw;
                            if (locality.get("PostalCode") == null) {
                                raw = locality.getString("LocalityName");
                            } else {
                                raw = locality.getJSONObject("PostalCode").getString("PostalCodeNumber");
                            }

                            sharedPreferences.edit().putString("Location", raw).commit();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        } catch (ClientProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        sharedPreferences.edit().putString("Location", "Miami").commit();
                    }

                    final WeatherInfo weatherInfo = WeatherProvider.getWeather("Miami");
                    if (weatherInfo != null) {
                        handler.post(new Runnable() {
                            public void run() {
                                render(weatherInfo);
                            }
                        });
                    }
                }
            }
        });
    }
}

From source file:com.androidquery.simplefeed.activity.PlaceActivity.java

private void refreshButton() {

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (enabled) {
        aq.id(R.id.button_gps).gone();/* w w  w. j  a va2  s  .co  m*/
    } else {
        aq.id(R.id.button_gps).visible();
    }

    refreshHint();

}

From source file:com.finlay.geomonsters.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "onCreated");
    super.onCreate(savedInstanceState);
    _activity = this;

    // set app to fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // layout//from   w ww  .ja  v  a 2s  . c  o  m
    setContentView(R.layout.activity_ranch);

    // Socket, Location Manager, Weather Manager
    locationListener = new MyLocationListener(this);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    socket = new SocketIO();
    weatherManager = new WeatherManager(this);

    // layout items
    theTextView = (TextView) findViewById(R.id.txtMessage);
    forceButton = (Button) findViewById(R.id.btnGetLocation);
    waitButton = (Button) findViewById(R.id.btnWaitLocation);
    loadEncounterButton = (Button) findViewById(R.id.btnLoadEncounter);

    // TODO get rid of this. For now, reset the encounters file whenever created
    ConfigManager.ResetConfigFiles(getApplicationContext());

    forceButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.v(TAG, "Force Click");

            // Ensure connected
            if (!isNetworkAvailable()) {
                setMessage("No internet connection.");
                return;
            }

            forceButton.setText("...");

            // Connect to server
            connectSocket();

            // Best provider
            Criteria criteria = new Criteria();
            String bestProvider = locationManager.getBestProvider(criteria, false);

            // Request location updates
            locationManager.requestLocationUpdates(bestProvider, 10000, 0, locationListener);

            forceButton.setEnabled(false);
            loadEncounterButton.setEnabled(false);
        }
    });

    waitButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.v(TAG, "Wait click");
            waitButton.setText("...");

            // Start Encounter Service
            // TODO Service should be started at boot?
            _activity.startService(new Intent(_activity, EncounterService.class));

            waitButton.setEnabled(false);
        }

    });

    loadEncounterButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.v(TAG, "Load encounter clicked");

            // Ensure connection
            if (!isNetworkAvailable()) {
                setMessage("No internet connection.");
                return;
            }

            // Pull encounter
            String encounter = ConfigManager.PullEncounter(getApplicationContext());
            if (encounter.equals(""))
                return;

            // Use location & time from gathered string to query encounter
            String[] location = encounter.split(",");
            String latitude = location[0];
            String longitude = location[1];
            // TODO We cannot get historical weatherdata accurately. So we will just use the current time :(

            // Pop used encounter from queue
            ConfigManager.PopEncounter(getApplicationContext());

            // Server & weather
            connectSocket();
            weatherManager.execute(longitude, latitude);
            while (!socket.isConnected())
                ;

            // Send query to server
            sendLocation(longitude, latitude);
        }
    });

    // Change the text value of the loadEncounterButton to the number of encounters available
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    loadEncounterButton.setText("" + ConfigManager.EncounterCount(getApplicationContext()));
                }
            });
        }
    }, 0, UPDATE_DELAY);
}

From source file:com.facebook.android.Places.java

public void getLocation() {
    /*//from   w  w w.  ja va 2s.  co m
     * launch a new Thread to get new location
     */
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            dialog = ProgressDialog.show(Places.this, "", getString(R.string.fetching_location), false, true,
                    new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            showToast("No location fetched.");
                        }
                    });

            if (lm == null) {
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            }

            if (locationListener == null) {
                locationListener = new MyLocationListener();
            }

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            if (provider != null && lm.isProviderEnabled(provider)) {
                lm.requestLocationUpdates(provider, 1, 0, locationListener, Looper.getMainLooper());
            } else {
                /*
                 * GPS not enabled, prompt user to enable GPS in the
                 * Location menu
                 */
                new AlertDialog.Builder(Places.this).setTitle(R.string.enable_gps_title)
                        .setMessage(getString(R.string.enable_gps))
                        .setPositiveButton(R.string.gps_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivityForResult(
                                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                                        0);
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Places.this.finish();
                            }
                        }).show();
            }
            Looper.loop();
        }
    }.start();
}

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

@Override
public void onCreate() {
    Log.d(MainActivity.TAG, "onCreate()");
    super.onCreate();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    trackerLocationListener = new TrackerLocationListener();
}

From source file:com.nextgis.maplibui.service.WalkEditService.java

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

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mSharedPreferencesTemp = getSharedPreferences(TEMP_PREFERENCES, Constants.MODE_MULTI_PROCESS);

    mTicker = getString(R.string.tracks_running);
    mSmallIcon = R.drawable.ic_action_maps_directions_walk;

    mLayerId = Constants.NOT_FOUND;/*from ww w  .jav a  2 s.c  o  m*/
}