java - How to ask multiple permissions at the same time in android 6.0+ -
i want ask user accept following permissions @ same time(one one), permissions like: checklocationpermission, checkreadsms, checkcallingpermission, checkreadstate, checkcontactwritestate. so, how can ask these permissions in first screen itself. please me out in regard. in advance.
you have first check user phone build version 23.
if (build.version.sdk_int >= build.version_codes.m) { askpermissions(true); } else { startactivity(new intent(permissionsactivity.this, splashactivity.class)); finish(); }
if version 23 need ask permissions.
private void askpermissions(boolean isforopen) { isrationale = false; list permissionsrequired = new arraylist(); final list<string> permissionslist = new arraylist<string>(); if (!checkpermission(permissionslist, manifest.permission.write_external_storage)) permissionsrequired.add("write external storage"); if (!checkpermission(permissionslist, manifest.permission.call_phone)) permissionsrequired.add("call phone"); if (!checkpermission(permissionslist, manifest.permission.read_phone_state)) permissionsrequired.add("read phone state"); if (!checkpermission(permissionslist, manifest.permission.read_contacts)) permissionsrequired.add("read contacts"); if (!checkpermission(permissionslist, manifest.permission.receive_sms)) permissionsrequired.add("receive sms"); if (!checkpermission(permissionslist, manifest.permission.get_accounts)) permissionsrequired.add("get accounts"); if (!checkpermission(permissionslist, manifest.permission.access_coarse_location)) permissionsrequired.add("location"); if (!checkpermission(permissionslist, manifest.permission.access_fine_location)) permissionsrequired.add("location"); if (permissionslist.size() > 0 && !isrationale) { if (permissionsrequired.size() > 0) { } if (isforopen) { if (build.version.sdk_int >= build.version_codes.m) { activitycompat.requestpermissions(this, permissionslist.toarray(new string[permissionslist.size()]), 11); } } } else if (isrationale) { if (isforopen) { new android.support.v7.app.alertdialog.builder(this, r.style.appcompatalertdialogstyle) .settitle("permission alert") .setmessage("you need grant permissions manually. go permission , grant permissions.") .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); intent intent = new intent(settings.action_application_details_settings); uri uri = uri.fromparts("package", getpackagename(), null); intent.setdata(uri); startactivityforresult(intent, 123); } }) .show(); } } else { startactivity(new intent(permissionsactivity.this, splashactivity.class)); finish(); } } private boolean checkpermission(list permissionslist, string permission) { if (build.version.sdk_int >= build.version_codes.m) { if (contextcompat.checkselfpermission(this, permission) != packagemanager.permission_granted) { permissionslist.add(permission); // check rationale option if (!isfirst) { if (!activitycompat.shouldshowrequestpermissionrationale(this, permission)) { isrationale = true; return false; } } } } return true; }
on onrequestpermissionsresult need check permissions granted
@override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { switch (requestcode) { case 11: map<string, integer> perms = new hashmap<string, integer>(); // initial perms.put(manifest.permission.write_external_storage, packagemanager.permission_granted); perms.put(manifest.permission.call_phone, packagemanager.permission_granted); perms.put(manifest.permission.read_phone_state, packagemanager.permission_granted); perms.put(manifest.permission.read_contacts, packagemanager.permission_granted); perms.put(manifest.permission.receive_sms, packagemanager.permission_granted); // fill results (int = 0; < permissions.length; i++) { perms.put(permissions[i], grantresults[i]); } // check access_fine_location if (perms.get(manifest.permission.write_external_storage) == packagemanager.permission_granted && perms.get(manifest.permission.call_phone) == packagemanager.permission_granted && perms.get(manifest.permission.read_phone_state) == packagemanager.permission_granted && perms.get(manifest.permission.read_contacts) == packagemanager.permission_granted && perms.get(manifest.permission.receive_sms) == packagemanager.permission_granted) { // permissions granted startactivity(new intent(permissionsactivity.this, splashactivity.class)); finish(); } else { // permission denied toast.maketext(this, "some permission denied.", toast.length_short) .show(); isfirst = false; askpermissions(true); } break; default: super.onrequestpermissionsresult(requestcode, permissions, grantresults); } }
if user has set permission never ask again application setting screen open. user allow/deny permission there. need check again on activityresult.
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); askpermissions(true); }
Comments
Post a Comment