Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to find user location using GPS
    text
    copied!<pre><code> public class LocationtesterActivity extends Activity { /** Called when the activity is first created. */ LocationManager locManager; // LocationListener locListener; Button b; String provider; TextView lat, alt, longi; // Here i am first checking if both GPS and network options are enabled in Lovation and Security Settings or not. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b = (Button) findViewById(R.id.button1); lat = (TextView) findViewById(R.id.lattitude); alt = (TextView) findViewById(R.id.altitude); longi = (TextView) findViewById(R.id.longitude); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showCurrentLocation(); } }); locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); provider = locManager.getBestProvider(criteria, true); System.out.println("best provider is :" + provider); if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || !locManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { System.out.println("in provider enablement"); createGpsDisabledAlert(); } else { System.out.println("in location update request"); locManager.requestLocationUpdates(provider, 0, 0, new MyLocationListener()); } } // for displaying the Dialogue Box // if GPS or network not enabled // and also taking to location and security screen private void createGpsDisabledAlert() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage( "Your GPS or network provider is disabled! Would you like to enable it?") .setCancelable(false) .setPositiveButton("Enable provider", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { showGpsOptions(); } }); builder.setNegativeButton("Do nothing", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } private void showGpsOptions() { Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(gpsOptionsIntent, 5); } // //////ends///// // Code to check whether user enabled GPS and Network provider in settings // if not then show the dialogue box again protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 5 &amp;&amp; resultCode == 0) { if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { createGpsDisabledAlert(); // Toast.makeText(this, // "provider not enabled. Click the button for settings", // 2000).show(); } else { Intent i=new Intent(this, LocationtesterActivity.class); startActivity(i); Toast.makeText(this, "User has enabled the provider", 1000) .show(); } } } // Method to display the current location protected void showCurrentLocation() { Location location = locManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { String message = String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() ); lat.setText("" + location.getLatitude()); longi.setText("" + location.getLongitude()); Toast.makeText(LocationtesterActivity.this, message, Toast.LENGTH_LONG).show(); } else { Toast.makeText(LocationtesterActivity.this, "no last known location", 1000).show(); } } // Inner class for LocationListener private class MyLocationListener implements LocationListener { public void onLocationChanged(Location location) { System.out.println("in location changed"); String message = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() ); lat.setText("" + location.getLatitude()); longi.setText("" + location.getLongitude()); // alt.setText("" + location.getAltitude()); Toast.makeText(LocationtesterActivity.this, message, Toast.LENGTH_LONG).show(); } public void onStatusChanged(String s, int i, Bundle b) { Toast.makeText(LocationtesterActivity.this, "Provider status changed", Toast.LENGTH_LONG).show(); } public void onProviderDisabled(String s) { Toast.makeText(LocationtesterActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show(); } public void onProviderEnabled(String s) { Toast.makeText(LocationtesterActivity.this, "Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show(); } } } </code></pre> <p>I am using the above code to find the User Location. 1.I am getting always GPS as the best provider according to my criteria but that is fine to me. 2.Thing is that why am i not able to get any location values ? Even with Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); i am not getting any thing. Please help me in getting the location values.</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