title: Android Geolocation working in emulator but not working in phone
body: <p>I made a simple app with geolocation that displays current location of the user in text views, like latitude, longitude and with <strong>Geocoder</strong> cityname countryname postal-code, etc. </p>
  
  <p>Everything works perfectly in the emulator but for some reason the location doesn't get retrieved in my mobile. </p>
  
  <p>The emulator is running android 7.1 and my mobile is running android 7.0 but that should not be a problem because I made the app with 6.0 marshmallow in mind. </p>
  
  <p><strong><em>Here is the code</em></strong> </p>
  
  <p><strong>"Mainactivity"</strong></p>
  
  <pre><code>package com.example.slimshady.LocationInfoApp;
  
  import android.Manifest;
  import android.content.Context;
  import android.content.pm.PackageManager;
  import android.location.Address;
  import android.location.Geocoder;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Build;
  import android.support.annotation.NonNull;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  
  import java.util.List;
  import java.util.Locale;
  
  public class MainActivity extends AppCompatActivity {
  
      LocationManager locationManager;
      LocationListener locationListener;
      Geocoder geocoder;
      TextView latitude, longitude, city, postcode, country;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          latitude = (TextView)findViewById(R.id.latitude);
          longitude = (TextView) findViewById(R.id.longitude);
          city = (TextView)findViewById(R.id.city);
          postcode = (TextView)findViewById(R.id.postcode);
          country = (TextView)findViewById(R.id.country);
  
  
          locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
          locationListener = new LocationListener() {
              @Override
              public void onLocationChanged(Location location) {
  
                  updateLocationInfo(location);
  
              }
  
              @Override
              public void onStatusChanged(String s, int i, Bundle bundle) {
  
              }
  
              @Override
              public void onProviderEnabled(String s) {
  
              }
  
              @Override
              public void onProviderDisabled(String s) {
  
              }
          };
  
          // asking permission starts here
          if (Build.VERSION.SDK_INT &lt; 23){
  
              // api level lower than 23 so no need to ask for permission
  
              try {
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
  
              }
              catch (SecurityException e){
  
                  e.printStackTrace();
              }
          }
          else {
  
              // api level greater than or equal to 23 checking if has permission if not VVVV this condition
              // means go ask for permission
  
              if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
  
                  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
              }
              else{
  
                  // means api level greater than or equal to 23 and already has permission
                  // so no need to go out and ask for permission
  
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
  
                  // last known location because we went to this part means we have been here before
                  Location lastKnownLocation =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                  if (lastKnownLocation != null) {
                      updateLocationInfo(lastKnownLocation);
                  }
  
  
  
              }
  
          }
          // asking permission ends here
  
  
      }
  
      public void updateLocationInfo(Location location) {
  
          geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
  
          try {
  
              List&lt;Address&gt; locationAddress = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(),1);
              if (locationAddress != null &amp;&amp; locationAddress.size() &gt; 0) {
  
                  latitude.setText("Latitude: "+String.valueOf(locationAddress.get(0).getLatitude()));
                  longitude.setText("Longitude: "+String.valueOf(locationAddress.get(0).getLongitude()));
                  city.setText("City: "+String.valueOf(locationAddress.get(0).getLocality()));
                  postcode.setText("Post Code: "+String.valueOf(locationAddress.get(0).getPostalCode()));
                  country.setText("Country: "+String.valueOf(locationAddress.get(0).getCountryName()));
  
              }
  
  
          }
          catch (Exception e){
  
          }
  
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  
          if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED){
  
              startListening();
  
          }
      }
  
      public void startListening(){
  
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
  
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
  
          }
  
      }
  }
  </code></pre>
  
  <p>The code is simple and basic, everything should work fine but for some reason it doesn't.</p>
  
  <p><strong>I mean the GPS receiver in my physical device works with google-maps so I know its not broken.</strong></p>
  
  <p><strong><em>See it works in the emulator fine</em></strong>
  <a href="https://i.stack.imgur.com/iLtQF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iLtQF.png" alt="Image of it working in the emulator"></a></p>
  
  <p>But the same code does not work on my mobile and I have added all the permissions:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p><strong>See the same app doesn't work on my physical device, this is a screenshot from my physical device</strong></p>
  
  <p><a href="https://i.stack.imgur.com/lmkev.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lmkev.jpg" alt="Screenshot of it not working on mobile"></a></p>
  
  <p>As you see the location is on and I have granted permission when asked in the popup.
  And last but not least to show that the GPS receiver works and is not damaged in my physical device. </p>
  
  <p>Screenshot of me using google maps
  <a href="https://i.stack.imgur.com/di3o4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/di3o4.jpg" alt="Screenshot of me using google maps"></a>
  <strong><em>I circled in red the GPS on and that maps can get my GPS location</em></strong></p>