title: Getting GPS location in Android
body: <p>I am trying to get the current GPS-Location via Android, but it doesn't work and shows error. I have also included the <code>runtime permission</code> required but it still doesn't work. </p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements LocationListener {
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
              != PackageManager.PERMISSION_GRANTED
              &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
              != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  
      }
  
  
  
  }
  
  @Override
  public void onLocationChanged(Location location) {
      double latitude=location.getLatitude();
      double longitude=location.getLongitude();
  
      Log.i("Geo_location","Latitude: "+latitude+" ,Longitude: "+longitude);
  }
  
  @Override
  public void onStatusChanged(String s, int i, Bundle bundle) {
  
  }
  
  @Override
  public void onProviderEnabled(String s) {
  
  }
  
  @Override
  public void onProviderDisabled(String s) {
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.shaloin.gps"&gt;
  
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>The error is: 
   <code>"gps" location provider requires ACCESS_FINE_LOCATION permission.</code>
   for which I have added the runtime permission as stated in this link <a href="https://developer.android.com/training/permissions/requesting.html" rel="nofollow noreferrer">Requesting Permissions at Run Time</a>. Please help. Thank you :)</p>