Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid GPS is returning null location
    text
    copied!<p>I have written code in that if GPS is disabled it will be enabled by code and try to get Location from gps but I am getting a null value. Below is my code </p> <pre><code> public void getValue() { LocationManager mlocManager = (LocationManager) MySettings.this.getSystemService(Context.LOCATION_SERVICE); boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER); System.out.println("GPS IS "+gpsEnabled); if (!gpsEnabled) { 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); } } SimpleDateFormat sdfDate = new SimpleDateFormat("MM/dd/yyyy"); try { getBatteryLevel(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, MySettings.this); Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); mLocation = location; if (location != null) { lat = location.getLatitude(); lon = location.getLongitude(); address = getAddress(); alt = location.getAltitude(); if (meterFootFlag) { diameter = location.getAccuracy(); } else diameter = location.getAccuracy() / 3.28084; } else { lat = 0.0; lon = 0.0; alt = 0.0; } } catch (Exception e) { lat = 0.0; lon = 0.0; alt = 0.0; } </code></pre> <p>Also I have added permission in manifest file</p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt; </code></pre> <p>but I am getting a null value for the location. </p> <p>Any ideas on how I can get the location?</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