Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 273
Rogerio Camorim Avatar asked Feb 04 '17 00:02

Rogerio Camorim


People also ask

What is the difference between request and permission?

Permission is to take one's consent. It is a formal action, whereas a request is a gentle and casual way to ask. A request can be asked face to face or on phone, while a permission could be face to face or a written letter. Here are some great tips on how to request someone.

How do I check if permission is granted Android?

checkSelfPermission(activity, Manifest. permission. X) checks if any permission is already granted, if you check out other answers they do the same, and rest of the code asks for the permissions not granted.


2 Answers

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);
}
like image 72
Vitor Hugo Schwaab Avatar answered Sep 21 '22 15:09

Vitor Hugo Schwaab


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.

like image 28
Domen Jakofčič Avatar answered Sep 20 '22 15:09

Domen Jakofčič