Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE); String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains("gps")){ //if gps is disabled final Intent poke = new Intent(); poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); poke.addCategory(Intent.CATEGORY_ALTERNATIVE); poke.setData(Uri.parse("3")); sendBroadcast(poke); } locationListener = new MyLocationListener(); locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, locationListener); </code></pre> <p>Now make <strong>MyLocationListener class</strong> in same activity.</p> <pre><code>private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { String longitude = "Longitude: " +loc.getLongitude(); String latitude = "Latitude: " +loc.getLatitude(); /*----------to get City-Name from coordinates ------------- */ String cityName=null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List&lt;Address&gt; addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() &gt; 0) { System.out.println(addresses.get(0).getLocality()); cityName=addresses.get(0).getLocality(); System.out.println("MESSAGE:"+cityName); } } catch (IOException e) { e.printStackTrace(); } String s = longitude+"\n"+latitude+"\t city name:"+cityName; Log.v("OUTPUT, s); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } </code></pre> <p>Run your application in <strong>Actual device</strong>.</p> <p>you can figure out that automatic start <strong>GPS</strong> and see <strong>console</strong> logcat.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload