Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

if(!hasPermissions(this, permissions)){
    ActivityCompat.requestPermissions(this, permissions, permissions_code);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...