java - Marshmallow permissions and explanation -
could explain me plaese how should explain user permission
i work camera2api , implement such snippet of code ask permishion dinamicly
private void opencamera(int width, int height) { setupcameraoutputs(width, height); camerahelper.configuretransform(width, height, textureview, previewsize, getactivity()); cameramanager manager = (cameramanager) getsystemservice(context.camera_service); try { if (!cameraopencloselock.tryacquire(2500, timeunit.milliseconds)) { throw new runtimeexception("time out waiting lock camera opening."); } if (activitycompat.checkselfpermission(this, manifest.permission.camera) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.camera)) { // show explanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. } else { // no explanation needed, can request permission. activitycompat.requestpermissions( getactivity(), new string[]{android.manifest.permission.camera}, my_permissions_request); // my_permissions_request // app-defined int constant. callback method gets // result of request. } } manager.opencamera(mcameraid, mstatecallback, mbackgroundhandler); } catch (cameraaccessexception e) { e.printstacktrace(); } catch (interruptedexception e) { throw new runtimeexception("interrupted while trying lock camera opening.", e); } }
according google docks should check if user deny permission before
there method return true of false
shouldshowrequestpermissionrationale();
and according google
this method returns true if app has requested permission , user denied request.
if understand correctly according google comments inside method implementation
// show explanation user asynchronously -- don't block
// thread waiting user's response! after user
// sees explanation, try again request permission.
and example user deny permission before , next time when go screen camera app should create castom pop-up explanation "please agree permishion if want continue" , example user agree time , should recall method again according this
// sees explanation, try again request permission.
but method
shouldshowrequestpermissionrationale();
return me true
again, because didn't know user intent agree permission.
could explain me please how make way? maybe have example?
eventually find solution halp of @nuuneoi, lot!
and implement this
public void camera(view view) { tocamera(); } private void tocamera() { if (!ischeckpermission()){ return; } if (isprocesswasfinish()) { startactivity(new intent(getapplicationcontext(), cameraactivity.class)); overridependingtransition(r.anim.open_next, r.anim.close_main); } else { startactivity(new intent(getapplicationcontext(), userdatascreen.class)); overridependingtransition(r.anim.open_next, r.anim.close_main); } } private boolean ischeckpermission() { if (activitycompat.checkselfpermission(this, manifest.permission.camera) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.camera)) { showmessageokcancel("you need allow access camera"); return false; } // no explanation needed, can request permission. activitycompat.requestpermissions(this, new string[]{android.manifest.permission.camera}, my_permissions_request); return false; // my_permissions_request // app-defined int constant. callback method gets // result of request. } return true; } private void showmessageokcancel(string message) { new alertdialog.builder(mainactivity.this) .setmessage(message) .setpositivebutton("ok", listener) .setnegativebutton("cancel", listener) .create() .show(); } dialoginterface.onclicklistener listener = new dialoginterface.onclicklistener() { final int button_negative = -2; final int button_positive = -1; @override public void onclick(dialoginterface dialog, int which) { switch (which) { case button_negative: // int = -2 dialog.dismiss(); break; case button_positive: // int = -1 activitycompat.requestpermissions( mainactivity.this, new string[]{android.manifest.permission.camera}, my_permissions_request); dialog.dismiss(); break; } } }; @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { switch (requestcode) { case my_permissions_request: { // if request cancelled, result arrays empty. if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { log.e(my_log, "camera permission granted"); // permission granted, yay! // contacts-related task need do. tocamera(); } else { log.e(my_log, "camera permission denied"); // permission denied, boo! disable // functionality depends on permission. } } default: { super.onrequestpermissionsresult(requestcode, permissions, grantresults); } // other 'case' lines check other // permissions app might request } }
Comments
Post a Comment