ActivityCompat.shouldShowRequestPermissionRationale
文章推薦指數: 80 %
for (String permission : permissions) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { HowtouseshouldShowRequestPermissionRationalemethodinandroid.support.v4.app.ActivityCompatBestJavacodesnippetsusingandroid.support.v4.app.ActivityCompat.shouldShowRequestPermissionRationale(Showingtop20resultsoutof1,881)/** *Checksgivenpermissionsareneededtoshowrationale. * *@returnreturnstrueifoneofthepermissionisneededtoshowrationale. */ staticbooleanshouldShowRequestPermissionRationale(Activityactivity,String...permissions){ for(Stringpermission:permissions){ if(ActivityCompat.shouldShowRequestPermissionRationale(activity,permission)){ returntrue; } } returnfalse; } privatebooleanshouldShowRequestPermissionRationale(){ returnActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); } /** *@seeActivityCompat#shouldShowRequestPermissionRationale */ booleanshouldShowRequestPermissionRationale(@NullableActivityactivity, @NonNullStringpermission){ if(activity==null){ returnfalse; } returnActivityCompat.shouldShowRequestPermissionRationale(activity,permission); } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) privatevoidrequestStoragePermission(){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){ ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_STORAGE); }else{ //Eh,promptanyway ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_STORAGE); } } publicvoidrunWithPermissionsCheck(intrequestCode,Stringpermission,Runnablerunnable){ if(ContextCompat.checkSelfPermission(this,permission)!=PackageManager.PERMISSION_GRANTED){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,permission)){ Toast.makeText(this,"pleasegivemethepermission",Toast.LENGTH_SHORT).show(); }else{ ActivityCompat.requestPermissions(this,newString[]{permission},requestCode); } }else{ if(runnable!=null){ runnable.run(); } } } publicstaticvoidcheckPermission(Activityactivity,Runnablerunnable){ //检查权限(NEED_PERMISSION)是否被授权PackageManager.PERMISSION_GRANTED表示同意授权 if(ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE) !=PackageManager.PERMISSION_GRANTED){ //用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释 if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission .WRITE_EXTERNAL_STORAGE)){ Toast.makeText(activity,"请开通相关权限,否则无法正常使用本应用!",Toast.LENGTH_SHORT).show(); } //申请权限 ActivityCompat.requestPermissions(activity,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); }else{ runnable.run(); //Toast.makeText(activity,"授权成功!",Toast.LENGTH_SHORT).show(); } } privatevoidrequestPermission(finalStringpermission,Stringrationale,finalintrequestCode){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,permission)){ newAlertDialog.Builder(this) .setTitle(R.string.mis_permission_dialog_title) .setMessage(rationale) .setPositiveButton(R.string.mis_permission_dialog_ok,newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ ActivityCompat.requestPermissions(MainActivity.this,newString[]{permission},requestCode); } }) .setNegativeButton(R.string.mis_permission_dialog_cancel,null) .create().show(); }else{ ActivityCompat.requestPermissions(this,newString[]{permission},requestCode); } } privatevoidrequestLocationPermission(){ /* *Promptstheusertograntlocationpermissions.Thissampleappusesthe *device's'locationtogetthecurrentplacefromthePlaceGraphSDK, *andtoperformlocalplacesearches. */ if(ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.ACCESS_FINE_LOCATION)){ AlertDialog.Builderbuilder=newAlertDialog.Builder(this); builder.setMessage(R.string.permission_prompt_location); builder.setPositiveButton(R.string.ok,newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ ActivityCompat.requestPermissions( MainActivity.this, newString[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION); } }); builder.create().show(); }else{ ActivityCompat.requestPermissions( this, newString[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION); } } /** *Handlestherequestingofthecamerapermission.Thisincludesshowinga"Snackbar"message *ofwhythepermissionisneededthensendingtherequest. */ privatevoidrequestCameraPermission(){ Log.w(TAG,"Camerapermissionisnotgranted.Requestingpermission"); finalString[]permissions=newString[]{Manifest.permission.CAMERA}; if(!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){ ActivityCompat.requestPermissions(this,permissions,RC_HANDLE_CAMERA_PERM); return; } finalActivitythisActivity=this; View.OnClickListenerlistener=newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ ActivityCompat.requestPermissions(thisActivity,permissions, RC_HANDLE_CAMERA_PERM); } }; Snackbar.make(mGraphicOverlay,R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok,listener) .show(); } /** *Handlestherequestingofthecamerapermission.Thisincludes *showinga"Snackbar"messageofwhythepermissionisneededthen *sendingtherequest. */ privatevoidrequestCameraPermission(){ Log.w(TAG,"Camerapermissionisnotgranted.Requestingpermission"); finalString[]permissions=newString[]{Manifest.permission.CAMERA}; if(!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){ ActivityCompat.requestPermissions(this,permissions,RC_HANDLE_CAMERA_PERM); return; } finalActivitythisActivity=this; View.OnClickListenerlistener=newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ ActivityCompat.requestPermissions(thisActivity,permissions, RC_HANDLE_CAMERA_PERM); } }; Snackbar.make(graphicOverlay,R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok,listener) .show(); } /** *Handlestherequestingofthecamerapermission.Thisincludes *showinga"Snackbar"messageofwhythepermissionisneededthen *sendingtherequest. */ privatevoidrequestCameraPermission(){ Log.w(TAG,"Camerapermissionisnotgranted.Requestingpermission"); finalString[]permissions=newString[]{Manifest.permission.CAMERA}; if(!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){ ActivityCompat.requestPermissions(this,permissions,RC_HANDLE_CAMERA_PERM); return; } finalActivitythisActivity=this; View.OnClickListenerlistener=newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ ActivityCompat.requestPermissions(thisActivity,permissions, RC_HANDLE_CAMERA_PERM); } }; Snackbar.make(mGraphicOverlay,R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok,listener) .show(); } /** *Handlestherequestingofthecamerapermission.Thisincludes *showinga"Snackbar"messageofwhythepermissionisneededthen *sendingtherequest. */ privatevoidrequestCameraPermission(){ Log.w(TAG,"Camerapermissionisnotgranted.Requestingpermission"); finalString[]permissions=newString[]{Manifest.permission.CAMERA}; if(!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){ ActivityCompat.requestPermissions(this,permissions,RC_HANDLE_CAMERA_PERM); return; } finalActivitythisActivity=this; View.OnClickListenerlistener=newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ ActivityCompat.requestPermissions(thisActivity,permissions, RC_HANDLE_CAMERA_PERM); } }; Snackbar.make(mGraphicOverlay,R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok,listener) .show(); } /** *@returntruefortherenoneedtorequest,doyourworkitnow. *falsefordoin{@linkandroid.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int,String[],int[])} */ publicstaticbooleanrequest(finalActivityactivity,finalStringpermission,Stringrationale,finalintrequestCode){ if(!(activityinstanceofActivityCompat.OnRequestPermissionsResultCallback)){ thrownewIllegalStateException("TheActivitymustimplementActivityCompat.OnRequestPermissionsResultCallback"); } if(ActivityCompat.checkSelfPermission(activity,permission) ==PackageManager.PERMISSION_GRANTED){ returntrue; } if(ActivityCompat.shouldShowRequestPermissionRationale(activity,permission)){ newAlertDialog.Builder(activity) .setMessage(rationale) .setPositiveButton(android.R.string.ok,newDialogInterface.OnClickListener(){ @Override publicvoidonClick(DialogInterfacedialog,intwhich){ requestPermissions(activity,newString[]{permission},requestCode); } }).setNegativeButton(android.R.string.cancel,null) .show(); }else{ returnrequestPermissions(activity,newString[]{permission},requestCode); } returnfalse; } }elseif(ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.CALL_PHONE)){ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(container=newFrameLayout(this)); setTitle("Original"); if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=PackageManager.PERMISSION_GRANTED){ //Shouldweshowanexplanation? if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){ Toast.makeText(this,"Cameraaccessisrequired.",Toast.LENGTH_SHORT).show(); }else{ ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); } }else{ setupCameraPreviewView(); } } publicstaticbooleannonShowRationale(PermissionWrapperwrapper){ return!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(wrapper),wrapper.getRequestPermission()); } @Override protectedvoidonResume(){ super.onResume(); if(ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA) ==PackageManager.PERMISSION_GRANTED){ mCameraView.start(); }elseif(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){ ConfirmationDialogFragment .newInstance(R.string.camera_permission_confirmation, newString[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION, R.string.camera_permission_not_granted) .show(getSupportFragmentManager(),FRAGMENT_DIALOG); }else{ ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); } } publicstaticvoidaskForPermission(finalActivityactivity,finalintrequestCode, finalStringpermission,@StringResfinalintaskTitle, @StringResfinalintaskContent){ if(ActivityCompat.shouldShowRequestPermissionRationale(activity,permission)){ MaterialDialog.Builderbuilder=newMaterialDialog.Builder(activity) .title(askTitle) .content(askContent) .positiveText(android.R.string.yes) .negativeText(android.R.string.no) .onPositive((dialog,which)-> ActivityCompat.requestPermissions(activity, newString[]{permission},requestCode)); builder.show(); }else{ ActivityCompat.requestPermissions(activity,newString[]{permission},requestCode); } } publicvoidrequestPermissionOrShowRationale(Permissionpermission){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,permission.permission)){ PermissionRationaleDialogFragmentdialogFragment= PermissionRationaleDialogFragment.newInstance(permission); dialogFragment.show(getSupportFragmentManager(),FRAGMENT_TAG_RATIONALE); }else{ requestPermission(permission); } } @SuppressWarnings("unchecked") voidtryRequestWithListener(){ PermissionCustomRationaleListenercustomRationaleListener=getPermissionCustomRationaleListener(); intrequestCode=getRequestCode(); if(customRationaleListener!=null){ customRationaleListener.permissionCustomRationale(requestCode); }else{ StringrequestPermission=getRequestPermission(); if(ActivityCompat.shouldShowRequestPermissionRationale(activity,requestPermission)){ PermissionRequestListenerrequestListener=getPermissionRequestListener(); if(requestListener!=null){ requestListener.permissionRationale(requestCode); } } ActivityCompat.requestPermissions(activity,newString[]{requestPermission}, requestCode); } } android.support.v4.appActivityCompatshouldShowRequestPermissionRationalePopularmethodsofActivityCompatrequestPermissionscheckSelfPermissionstartActivitystartActivityForResultStartnewactivitywithoptions,ifable,forwhichyouwouldlikearesultwhen itfinished.InAndrfinishAfterTransitionReversestheActivitySceneentryTransitionandtriggersthecallingActivity toreverseitsexitTfinishAffinityFinishthisactivity,andtriestofinishallactivitiesimmediatelybelowitin thecurrenttaskthgetColorpostponeEnterTransitionstartPostponedEnterTransitionsetEnterSharedElementCallbackWhen android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,android.view.View,String) wassetExitSharedElementCallbackWhen android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,android.view.View,String) wascreateCallbacksetExitSharedElementCallback,createCallback,invalidateOptionsMenu,startActivitiesPopularinJavaUpdatingdatabaseusingSQLpreparedstatementscheduleAtFixedRate(Timer)getSharedPreferences(Context)onRequestPermissionsResult(Fragment)ObjectMapper(com.fasterxml.jackson.databind)ObjectMapperprovidesfunctionalityforreadingandwritingJSON,eithertoand frombasicPOJOs(PlPrintWriter(java.io)WrapseitheranexistingOutputStreamoranexistingWriterandprovides conveniencemethodsforprinBigDecimal(java.math)Animmutablearbitrary-precisionsigneddecimal.Avalueisrepresentedbyan arbitrary-precision"unConnectException(java.net)AConnectExceptionisthrownifaconnectioncannotbeestablishedtoaremote hostonaspecificpoMalformedURLException(java.net)ThisexceptionisthrownwhenaprogramattemptstocreateanURLfroman incorrectspecification.Queue(java.util)Acollectiondesignedforholdingelementspriortoprocessing.Besidesbasic java.util.CollectionoToppluginsforWebStormProductsSearchforJavacodeSearchforJavaScriptcodeIDEPluginsIntelliJIDEAWebStormVisualStudioAndroidStudioEclipseVisualStudioCodePyCharmSublimeTextPhpStormVimGoLandRubyMineEmacsJupyterNotebookJupyterLabRiderDataGripAppCodeCompanyAboutUsContactUsCareersResourcesFAQBlogTabnineAcademyTermsofusePrivacypolicyJavaCodeIndexJavascriptCodeIndexGetTabnineforyourIDEnow
延伸文章資訊
- 1Android M Permissions : Confused on the usage of ...
It talks about the shouldShowRequestPermissionRationale() function which returns true if the app ...
- 2Android Permission 开发记录 - 掘金
shouldShowRequestPermissionRationale=false; 第一次请求权限被禁止,但未选择【不再提醒】 ActivityCompat.shouldShowReques...
- 3ActivityCompat - Android Developers
Call shouldShowRequestPermissionRationale before calling this API to check if the system recommen...
- 4关于shouldShowRequestPermissionRationale 详解 ...
Android M 权限:关于shouldShowRequestPermissionRationale 详解. 独家号 AndroidKy的独家号 作者 AndroidKy. 众所周知,在And...
- 5Android中使用shouldShowRequestPermissionRationale判断 ...
shouldShowRequestPermissionRationale字面意思看是应该显示请求权限原因已经通过的权限,方法返回为true 禁止权限,方法返回为true 禁止且不再询问权限, ...