title: Android check permission for LocationManager
body: <p>I'm trying to get the GPS coordinates to display when I click a button in my activity layout. The following is the method that gets called when I click the button:</p>
  
  <pre><code>public void getLocation(View view) {
      TextView tv = (TextView) findViewById(R.id.gps_coord_view);
      LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
      Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      tv.setText("Latitude: " + loc.getLatitude() + "\nLongitude: " + loc.getLongitude());
  }
  </code></pre>
  
  <p>I'm getting an error that says </p>
  
  <blockquote>
    <p>Call requires permission which may be rejected by user. Code should explicitly check to see if permission is available.</p>
  </blockquote>
  
  <p>I have already granted these permissions in my <code>AndroidManifest</code>. The error is taken care of and the app compiles when I add the following before calling <code>lm.getLastKnownLocation</code>:</p>
  
  <pre><code>if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
          &amp;&amp; checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      return;
  }
  </code></pre>
  
  <p>However, the app crashes when I press the button that calls getLocation when it's clicked. What is going on? Is there  better/simpler way to grab the GPS coordinates of the device? </p>