16

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?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Rogerio Camorim
  • 331
  • 1
  • 4
  • 14

7 Answers7

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);
}
Vitor Hugo Schwaab
  • 1,545
  • 20
  • 31
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.

Chirag Jain
  • 628
  • 11
  • 24
Domen Jakofčič
  • 626
  • 1
  • 8
  • 24
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();
 }

}
Aritra Basu
  • 43
  • 1
  • 8
  • 3
    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.) – Graeme Gill Dec 10 '18 at 11:35
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.

Mathieu
  • 1,435
  • 3
  • 16
  • 35
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.

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;
            }
        }
    }
-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/>
ghayas
  • 1
  • 4