Getting W/Activity: Can request only one set of permissions at a time
Asked Answered
S

7

20

I made an app that have a request for camera and GPS, but when I execute I am getting this warning several times with less than 1 second of each other.

W/Activity: Can reqeust only one set of permissions at a time)

Can some one tell me why?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    mStatusCamera = intent.getStringExtra("camera");
    mScannerView = new ZXingScannerView(this) {

        @Override
        protected IViewFinder createViewFinderView(Context context) {
            return new CustomZXingScannerView(context);
        }

    };
    List<BarcodeFormat> formats = new ArrayList<>();
    mListaPassageiros = new ArrayList<>();
    formats.add(BarcodeFormat.QR_CODE);
    setContentView(mScannerView);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
        if (!checkPermission()) {
            requestPermission();
        } else {
            executarDepoisDaPermissao();
        }
    }
}
private boolean checkPermission() {
    return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED
    && ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
    && ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}

public void executarDepoisDaPermissao() {
    final BancoController crud = new BancoController(getBaseContext());
    mConectado = isNetworkAvailable();
}

Added RequestPermissio as requested.

 private void requestPermission() {
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
        if (!checkPermission()) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION}, ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
        }
    }

}

Can I use it that way?

Sandal answered 4/2, 2017 at 0:44 Comment(1)
Can youshow requestPermission method?Hasidism
H
9

I think problem is that you ask for two location permissions, you should ask only for fine location which will work for both coarse and fine.

Hurds answered 4/2, 2017 at 1:55 Comment(3)
My computer is off now. Will try this latter.Sandal
I could not wait. Yes, that was the problem. ThxSandal
But sometimes you need permissions that are mutually exclusive. For that, look for answer by Vitor Hugo SchwaabAlicealicea
V
13

So, I can't see your requestPermission() method from here, but you shouldn't send multiple permission requests in the same time.

You should make ONE request with ALL the permissions.

Kotlin:

val permissionsCode = 42
val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
if (!hasPermissions(this, permissions)) {
    ActivityCompat.requestPermissions(this, permissions, permissionsCode)
}

Java:

int permissionsCode = 42; 
String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};

if (!hasPermissions(this, permissions)) {
    ActivityCompat.requestPermissions(this, permissions, permissionsCode);
}
Vender answered 4/2, 2017 at 1:36 Comment(3)
Almos the same thing I have, isn't is?Sandal
Almost. But Android will show only ONE beautiful and well animated permission dialog. I presume that's the goal. Dialog over dialog is considered a bad interface practice.Vender
1 At time, but will have like a arrow to the next. But i am not displaying a Dialog over other. Or I am, and i can't see it done?Sandal
H
9

I think problem is that you ask for two location permissions, you should ask only for fine location which will work for both coarse and fine.

Hurds answered 4/2, 2017 at 1:55 Comment(3)
My computer is off now. Will try this latter.Sandal
I could not wait. Yes, that was the problem. ThxSandal
But sometimes you need permissions that are mutually exclusive. For that, look for answer by Vitor Hugo SchwaabAlicealicea
B
3

For anyone else stumbling upon this issue..You need to request permissions serially,like this:

onRequestPermissionResult(){
case permission1:
 if (permission1.aquired()){
....//do what you do
 requestPermission2();
 }
 case permission2:
 if (permission2.aquired()){
....//do what you do
 requestPermission3();
 }

}
Biradial answered 22/9, 2017 at 7:5 Comment(1)
Yep - if your code can't be arranged to ask for all permissions in one call, then you have to write a bunch of ugly code to serialize your permission requests, so that there is only one outstanding call at a time.(It would be much better if Android took care of this, rather than each application having to reinvent the wheel.)Tsar
C
1

I had the same error, because I was calling the function that request permissions in the onCreate AND in the onResume. I thing it created two instance of permissions request, and Android system does not want that.

Then, you should remove the permissions request from your onResume (or from any other function that would run at the same time than your Activity onCreate.

Cetology answered 6/2, 2021 at 14:7 Comment(0)
K
0

The issue may be reproduce if you are trying to get the permission in a loop -

do {
 ActivityCompat.requestPermissions...
}while(some_condition)

In this case if condition is failed it may again request the same. I was able to reproduce this issue with same set of instruction.

Kampong answered 21/12, 2020 at 12:28 Comment(1)
Why should you ever want to do this?! Doesn't make any sense to me.Bum
R
0

My code for check permissions for acess camera and files.

protected void checkPermission(){
        if(ContextCompat.checkSelfPermission(
                context, Manifest.permission.READ_EXTERNAL_STORAGE)
                + ContextCompat.checkSelfPermission(
                context,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                + ContextCompat.checkSelfPermission(
                context, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED){

            // Do something, when permissions not granted
            ActivityCompat.requestPermissions(
                    context,
                    new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA
                    },
                    MY_PERMISSIONS_REQUEST_CODE
            );

        }else {
            return;
        }
    }

For results e manage this :

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CODE: {
                if (
                        (grantResults.length > 0) &&
                                (grantResults[0]
                                        == PackageManager.PERMISSION_GRANTED
                                )
                ) {
                    return;
                } else {
                    // permission was not granted
                    if (ActivityCompat.shouldShowRequestPermissionRationale(
                            context, Manifest.permission.READ_EXTERNAL_STORAGE)
                            || ActivityCompat.shouldShowRequestPermissionRationale(
                            context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            || ActivityCompat.shouldShowRequestPermissionRationale(
                            context, Manifest.permission.CAMERA)) {
                        // user denied , but not permanently
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setIcon(R.drawable.ic_receipt);
                        builder.setMessage("Ops... preciso da sua permissão para incluir sua foto.");
                        builder.setTitle("Atenção");
                        builder.setPositiveButton("OK", (dialogInterface, i) -> ActivityCompat.requestPermissions(
                                context,
                                new String[]{
                                        Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                        Manifest.permission.CAMERA
                                },
                                MY_PERMISSIONS_REQUEST_CODE
                        ));
                        builder.setNeutralButton("Cancelar", null);
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    } else {
                        Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "You denied permission.\n" +
                                "Em Configurações vá em > Permissões > E ative as permissões", Snackbar.LENGTH_LONG).setAction("Configurações", view -> startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + <**********yourpackagename*************> ))));
                        View snackbarView = snackbar.getView();
                        TextView textView = (TextView) snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
                        textView.setMaxLines(5);  //Or as much as you need
                        snackbar.show();
                    }
                }
                break;
            }
        }
    }
Relate answered 15/12, 2021 at 23:11 Comment(0)
D
-1

just add the permission in the manifest file and refresh the project

add this code in manifest XML File:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION/>
Demolish answered 25/5, 2020 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.