0

I am writing a code to get lastknownlocation from location manager and this prompted me to add runtime permissions.

Here is my code:

public class MainActivity extends AppCompatActivity implements LocationListener {

    LocationManager locationManager;
    String provider;
    private final int MY_PERMISSIONS_REQUEST_CODE=1;
    Location location;
    Boolean isPermissionGranted=false;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        provider = locationManager.getBestProvider(new Criteria(), false);


       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
               requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},MY_PERMISSIONS_REQUEST_CODE);
           }

          // return;
        }

    }

    public void getlastknownposition()
    {

    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);


        if(requestCode == MY_PERMISSIONS_REQUEST_CODE)
        {
            if(grantResults[0]==PackageManager.PERMISSION_GRANTED && grantResults[1]==PackageManager.PERMISSION_GRANTED){

            Toast.makeText(MainActivity.this," granted "+grantResults[0]+"granted2"+grantResults[1], Toast.LENGTH_SHORT).show();


            location = locationManager.getLastKnownLocation(provider);}

        }

    }

}

I am still getting an error "Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException in onRequestPermissionsResult on line location = locationManager.getLastKnownLocation(provider);

Abdul Wahab
  • 137
  • 2
  • 11
  • have you tried wrapping that line with a `if (ActivityCompat.checkSelfPermission`.. I think the error message is pretty explicit - just because you are making the call from within `onRequestPermissionResult` doesn't mean you don't have to check. You could also wrap that line in a `try/catch` - again the error is explicit.. you need to be catching a potential `SecurityException`. – trooper Oct 24 '16 at 17:23
  • You should probably change `requestPermissions` to `ActivityCompat.requestPermissions`. – kimbaudi Oct 24 '16 at 19:17
  • Try this it may be work http://stackoverflow.com/a/41221852/5488468 – Bipin Bharti Jan 03 '17 at 10:57

2 Answers2

0

Put some braces around the second if in onPermissionsResult =)

if(requestCode == MY_PERMISSIONS_REQUEST_CODE){
        if(grantResults[0]==PackageManager.PERMISSION_GRANTED && grantResults[1]==PackageManager.PERMISSION_GRANTED){
             Toast.makeText(MainActivity.this," granted "+grantResults[0]+"granted2"+grantResults[1], Toast.LENGTH_SHORT).show();

             location = locationManager.getLastKnownLocation(provider);
        } else {
              //TODO handle user saying NO! :)
        }
    }

Tip: if you request FINE_LOCATION you don't need COARSE, it already includes it.

Alqueraf
  • 1,148
  • 1
  • 11
  • 26
  • Surround it with try/catch as suggested in the comment then, I checked some project I had, I'm actually calling a method, and that method can throw a security exception. I can't think of any other workaround. – Alqueraf Oct 24 '16 at 17:32
0

Replace the line

location = locationManager.getLastKnownLocation(provider);

with

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
    location = locationManager.getLastKnownLocation(provider);
}
kimbaudi
  • 13,655
  • 9
  • 62
  • 74