Mike Kng
Mike Kng

Reputation: 285

Wait for user permission to continue

I want to have my app waited for the user to give the permission before the app continues. I searched other threads about it and found out that the common way would be to use onRequestPermissionResult. Now I‘m pretty clueless how to set it up: do I just put my program code into the onRequestPermissionResult except the request itself or how do I need to set it up, so the user is asked for it and the app continues afterwards?

This is my code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

   if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
           != PackageManager.PERMISSION_GRANTED) {

       ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
   } else {
       progressBar.setVisibility(View.VISIBLE);
       GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());
       Location location = gt.getLocation();

       if (location == null) {
           Toast.makeText(getActivity(), "No location found.",
                   Toast.LENGTH_LONG).show();
       } else {
           lat = location.getLatitude();
           lon = location.getLongitude();
       }
       new GetContacts().execute();
   }
} else {
  progressBar.setVisibility(View.VISIBLE);
  GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());
  Location location = gt.getLocation();

  if (location == null) {
     Toast.makeText(getActivity(), "No location found.",
              Toast.LENGTH_LONG).show();
  } else {
      lat = location.getLatitude();
      lon = location.getLongitude();
  }
    new GetContacts().execute();
}

And the RequestResult:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 123: {
            // If request is cancelled, the result arrays are empty
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                progressBar.setVisibility(View.VISIBLE);
                GPSTracker gt = new GPSTracker(getActivity().getApplicationContext());
                Location location = gt.getLocation();

                if (location == null) {
                    Toast.makeText(getActivity(), "No location found.",
                            Toast.LENGTH_LONG).show();
                } else {
                    lat = location.getLatitude();
                    lon = location.getLongitude();
                }
                new GetContacts().execute();

            } else {
                //Toast
            }
            return;
        }
    }
}

Upvotes: 1

Views: 595

Answers (1)

Mr.O
Mr.O

Reputation: 843

Like Martin Zeitler mentioned in the comments, you have to prepare for both APIs above and below Marshmallow such as this:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

    if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
     != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);
    } else {

        //GPS method
    }
} else {

    //GPS method
}

and exactly like you mentioned in your question in onRequestResult:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 123: {
            // If request is cancelled, the result arrays are empty
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //GPS method
            } else {
                // Toast for user
            }
            return;
        }
    }
}

Upvotes: 1

Related Questions