Asking for permissions at run-time, Android M+
Asked Answered
B

3

7

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.

Bagwell answered 5/3, 2017 at 13:10 Comment(2)
Try fully uninstalling your app, then reinstalling it. Perhaps in testing you wound up denying the permission twice when asked, after which Android will stop showing the dialog.Cyndy
Just tried this and unfortunately it hasn't resolved the problem.Bagwell
U
3
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);
    }
}
Unreasoning answered 5/3, 2017 at 13:43 Comment(3)
This still gives me the exact same problem. No dialog box.Bagwell
Please check that your device supports runtime permissions. (Try with another new device)Unreasoning
Thanks but I was able to fix it. I posted my solution here.Bagwell
B
0

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);
Bagwell answered 5/3, 2017 at 14:38 Comment(0)
H
0

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);
    }

}
Habiliment answered 11/4, 2017 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.