关于ZAKER 融媒体解决方案 合作 加入

android- 如何授予棉花糖运行时权限以从 Eclip.

CocoaChina 10-22

我已经应用了多个代码 , 但是它告诉我 checkSelfPermission ( ) 和 shouldShowRequestPermissionRationale 方法没有退出 , 请帮助我 .

我使用 brodcast 接收器类来读取 SMS, 它对于豆形软糖的脂聚来说是很好的选择 , 但是对于棉花糖和比软糖更低的版本 ( 如 Ice Cream Sandwitch ) 来说却不能 , 我如何使应用程序与所有android版本兼容 ,

请逐步指导我 , 因为我对此并不陌生 , 也请给我一个例子 .

听说是我的读短信许可代码 .

public void getpermission ( ) { // Here, thisActivity is the current activity if ( ContextCompat.checkSelfPermission ( MainActivity, Manifest.permission.READ_SMS ) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions ( thisActivity, new String [ ] {Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_SINGLE_PERMISSION ) ; // REQUEST_CODE_ASK_SINGLE_PERMISSION is an // app-defined int constant. The callback method gets the // result of the request. } }

最佳答案

您可以通过在 Activity 中扩展此给定的类来做到这一点 ,

public class BasePermissionAppCompatActivity extends AppCompatActivity {private final static String APP_NAME = "APP_NAME";private final static int REQUEST_READ_SMS_PERMISSION = 3004;public final static String READ_SMS_PERMISSION_NOT_GRANTED = "Please allow " + APP_NAME + " to access your SMS from setting";RequestPermissionAction onPermissionCallBack;private boolean checkReadSMSPermission ( ) { if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) { if ( checkSelfPermission ( Manifest.permission.READ_SMS ) == PackageManager.PERMISSION_GRANTED ) { return true; } else { return false; } } else { return true; }}public void getReadSMSPermission ( RequestPermissionAction onPermissionCallBack ) { this.onPermissionCallBack = onPermissionCallBack; if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) { if ( !checkReadSMSPermission ( ) ) { requestPermissions ( new String [ ] {Manifest.permission.READ_SMS}, REQUEST_READ_SMS_PERMISSION ) ; return; } } if ( onPermissionCallBack != null ) onPermissionCallBack.permissionGranted ( ) ;}@Overridepublic void onRequestPermissionsResult ( int requestCode, String [ ] permissions, int [ ] grantResults ) { super.onRequestPermissionsResult ( requestCode, permissions, grantResults ) ; if ( grantResults [ 0 ] == PackageManager.PERMISSION_GRANTED ) { if ( REQUEST_READ_SMS_PERMISSION == requestCode ) { // TODO Request Granted for READ_SMS. System.out.println ( "REQUEST_READ_SMS_PERMISSION Permission Granted" ) ; } if ( onPermissionCallBack != null ) onPermissionCallBack.permissionGranted ( ) ; } else if ( grantResults [ 0 ] == PackageManager.PERMISSION_DENIED ) { if ( REQUEST_READ_SMS_PERMISSION == requestCode ) { // TODO REQUEST_READ_SMS_PERMISSION Permission is not Granted. // TODO Request Not Granted. // This code is for get permission from setting. //final Intent i = new Intent ( ) ; //i.setAction ( Settings.ACTION_APPLICATION_DETAILS_SETTINGS ) ; //i.addCategory ( Intent.CATEGORY_DEFAULT ) ; //i.setData ( Uri.parse ( "package:" + getPackageName ( ) ) ) ; //i.addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK ) ; //i.addFlags ( Intent.FLAG_ACTIVITY_NO_HISTORY ) ; //i.addFlags ( Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ) ; //startActivity ( i ) ; } if ( onPermissionCallBack != null ) onPermissionCallBack.permissionDenied ( ) ; }}public interface RequestPermissionAction { void permissionDenied ( ) ; void permissionGranted ( ) ;}}

像这样

public class ActivityMain extends BasePermissionAppCompatActivity

现在在您的 ActivityMain 中 , 您可以通过此代码块获得许可

getReadSMSPermission ( new RequestPermissionAction ( ) {@Overridepublic void permissionDenied ( ) { // Call Back, when permission is Denied // TODO, task after permission is not greante}@Overridepublic void permissionGranted ( ) { // Call Back, when permission is Granted // TODO, task after permission is greante}} ) ;

还有一件事 , 您需要在清单中添加权限

<uses-permission android:name="android.permission.READ_SMS"/>

以上内容由"CocoaChina"上传发布 查看原文

觉得文章不错,微信扫描分享好友

扫码分享