Request for runtime permissions, Android M +

Firstly, I know that this is a duplicate question, but I was above the answers to other similar questions and could not find success with any of these solutions.

I developed an application that works fine on my testing device, which is Samsung S4 running Android L (5.0.1), however I would like this application to also work on newer versions of Android.

I understand that requesting permissions have changed for Android M +, so they need to be asked at runtime, but when I try to implement this, a dialog never appears and therefore the required permissions are never requested.

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 permission, if not already provided:

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 a Google 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, a dialog asking for permission never appears, but my console tells me what ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);is being called, and after many studies I’m still not quite sure why it doesn’t work. The only thing that, in my opinion, may be due to the fact that this Activity is a Fragment action that displays a Google Maps map. I tried to request permissions in another action, but the same thing will happen.

Thank.

EDIT

So, I just tested the code that I posted here in a completely new project, and it went fine and showed a dialog box. I am now very confused by what is preventing the display of this dialog box in my current project.

+4
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);
    }
}
+1

!

. , , , , , . , MapsActivity, , MapsActivity , .

, 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);
0

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

}
0
source

Source: https://habr.com/ru/post/1671510/


All Articles