Asking for permissions at run-time, Android M+

2019-02-22 20:25发布

问题:

Firstly, I know this is a duplicate question, but I've been over the answers from other similar questions and haven't been able to find success with any of those solutions.

I've developed an app which works perfectly on my testing device which is a Samsung S4 running Android L (5.0.1) however I would like for this app to also work on newer versions of Android.

I understand that requesting permissions have changed for Android M+ so that they have to be asked at run-time, but when I try to implement this the dialog box never appears and hence the required permissions are never asked for.

The permission I am requesting is ACCESS_FINE_LOCATION.

Here is the relevant code from my manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Where I ask for permission if it is not already granted:

if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

    startGeofenceMonitoring();

} else {
    askForPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
}

askForPermission function (slightly modified from Google's example):

private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(MapsActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);

        } else {

            Log.d(TAG, "askForPermission: " + permission);

            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
        }
    } else {
        Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
    }
}

And the onRequestPermissionsResult function:

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

    Log.d(TAG, "onRequestPermissionsResult: " + requestCode);

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Log.d(TAG, "onRequestPermissionsResult: request");

                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();

                    startGeofenceMonitoring();
                }
                break;
        }
}

As I said, the dialog box which is asking for the permissions never appears, but my console tells me that ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode); is called, and after a lot of research I'm still not really sure why it isn't working. The only thing that I think it could be due to is that this Activity is a Fragment activity displaying a Google Maps map. I did try to request the permissions in another activity but the exact same thing happens.

Thanks.

EDIT

So I just tested the code I posted here in a completely fresh project and it ran just fine and showed the dialog box. I am now really confused as to what is preventing this dialog box from being shown in my current project.

回答1:

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

private void requestPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
        ActivityCompat
                .requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}


回答2:

I managed to fix it!

In my app I have used a different activity to control tabs and which activity is being displayed to the user. This is the very first activity to be displayed and I think it is the only activity displayed throughout the whole program but just shows the content of the other activities. So when I requested the permissions for the MapsActivity, it wasn't showing because the MapsActivity wasn't the one actually being present.

My explanation may be wrong so here is the code for my TabBarActivity:

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, SecondActivity.class);
    spec = tabHost.newTabSpec("Second").setIndicator("Profile")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MapsActivity.class);
    spec = tabHost.newTabSpec("First").setIndicator("Location")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, ThirdActivity.class);
    spec = tabHost.newTabSpec("Third").setIndicator("Settings")
            .setContent(intent);
    tabHost.addTab(spec);


回答3:

The below code worked for me.

public static final int MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE=1;

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_tracks,container,false);

    getPermission();
        ...
          }

 public void getPermission(){
        if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)){

            }else{
                ActivityCompat.requestPermissions(getActivity(),new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
            }
        }else{
            getSongDetails();
        }
    }


 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case -1:
            //hello
            break;
        case MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE:
            if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                getPermission();
            }else{
                Toast.makeText(getActivity(),"You've denied permission",Toast.LENGTH_LONG).show();
            }
            return;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}