Android棉花糖请求权限?

我目前正在处理一个需要几个“危险”权限的应用程序。 所以我试着按照Android的棉花糖(API Level 23)的要求添加“请求权限”,但是找不到如何去做。

如何在我的应用程序中使用新的权限模式请求权限?

使用下面的代码打开一个对话框:

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 

获取活动结果如下:

 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); } return; } // other 'case' lines to check for other // permissions this app might request } } 

更多信息: https : //developer.android.com/training/permissions/requesting.html

我正在使用这个结构来检查我的应用程序是否有权限,如果没有权限请求。 所以在我的主要代码,我想检查下面写:

 int MyVersion = Build.VERSION.SDK_INT; if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { if (!checkIfAlreadyhavePermission()) { requestForSpecificPermission(); } } 

模块checkIfAlreadyhavePermission()被实现为:

 private boolean checkIfAlreadyhavePermission() { int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS); if (result == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } 

模块requestForSpecificPermission()被实现为:

 private void requestForSpecificPermission() { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS, Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101); } 

并在活动中覆盖:

 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 101: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { //granted } else { //not granted } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 

请参阅此链接了解更多详情: http : //revisitingandroid.blogspot.in/2017/01/how-to-check-and-request-for-run-time.html

从android棉花糖开始,我们需要向用户请求特定的权限,我们也可以通过代码检查是否已经给出权限。 看看最常用的权限列表

android.permission.Group.CALENDAR android.permission.READ_CALENDAR android.permission.WRITE_CALENDAR

android.permission-group.CAMERA android.permission.CAMERA

android.permission.GET_CONTACTS android.permission.READ_CONTACTS android.permission.READ_CONTACTS android.permission.GET_ACCOUNTS

android.permission-GROUP.LOCATION android.permission.ACCESS_FINE_LOCATION android.permission.ACCESS_COARSE_LOCATION

android.permission-group.MICROPHONE android.permission.RECORD_AUDIO

android.permission.READ_PHONE_STATE android.permission.CALL_PHONE android.permission.READ_CALL_LOG android.permission.WRITE_CALL_LOG android.permission.WRITE_CALL_LOG android.permission.ADD_VOICEMAIL android.permission.USE_SIP android.permission.PROCESS_OUTGOING_CALLS

android.permission-group.SENSORS android.permission.BODY_SENSORS

android.permission.GET_SMS android.permission.RECEIVE_SMS android.permission.RECEIVE_SMS android.permission.READ_SMS android.permission.RECEIVE_WAP_PUSH android.permission.RECEIVE_MMS android.permission.READ_CELL_BROADCASTS

android.permission-group.STORAGE android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE

这里是样本,如果我们需要检查任何权限

 if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.WRITE_CALENDAR)) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); alertBuilder.setCancelable(true); alertBuilder.setMessage("Write calendar permission is necessary to write event!!!"); alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); } }); } else { ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); } } 

我用这个包装(推荐)由谷歌开发者写的。 它超级简单易用。

https://github.com/googlesamples/easypermissions

function处理检查和要求许可,如果需要

 public void locationAndContactsTask() { String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS }; if (EasyPermissions.hasPermissions(this, perms)) { // Have permissions, do the thing! Toast.makeText(this, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show(); } else { // Ask for both permissions EasyPermissions.requestPermissions(this, getString(R.string.rationale_location_contacts), RC_LOCATION_CONTACTS_PERM, perms); } } 

快乐编码:)

Android-M,即API 23引入了运行时权限,以减lessAndroid设备中的安全漏洞,用户现在可以直接在运行时pipe理应用程序权限。如果用户拒绝您的应用程序的特定权限,则必须通过询问权限对话框你在查询中提到。

因此,检查行动之前,即检查您有权限访问资源链接 ,如果您的应用程序没有特定的权限,您可以请求权限链接,并处理下面的权限请求响应。

 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } 

所以最后,如果您计划使用新版本来避免强行closures,那么通过行为更改是一个很好的做法。

权限最佳实践。

你可以在这里通过官方的示例应用程序。

从Android棉花糖(API 23)及以上默认情况下,所有危险的权限(根据官方文档官方文档 )禁用。 安装完第一次打开应用程序后,您必须在运行时授予权限。

我做到了这一点:

 public class MarshMallowPermission { public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE_BY_GALLERY = 0; public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE_BY_CAMERA = 1; public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE_BY_LOAD_PROFILE = 2; public static final int CAMERA_PERMISSION_REQUEST_CODE = 3; public static final int LOCATION_PERMISSION_REQUEST_CODE = 4; Activity activity; Context mContext; public MarshMallowPermission(Activity activity) { this.activity = activity; this.mContext = activity; } public boolean checkPermissionForExternalStorage(){ int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (result == PackageManager.PERMISSION_GRANTED){ return true; } else { return false; } } public boolean checkPermissionForCamera(){ int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA); if (result == PackageManager.PERMISSION_GRANTED){ return true; } else { return false; } } public boolean checkLocationPermission(){ int result = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION); if (result == PackageManager.PERMISSION_GRANTED){ return true; } else { return false; } } public void requestPermissionForExternalStorage(int requestCode){ if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ Toast.makeText(mContext.getApplicationContext(), "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode); } } public void requestPermissionForCamera(){ if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){ Toast.makeText(mContext.getApplicationContext(), "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE); } } public void requestPermissionForLocation(){ if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_COARSE_LOCATION)){ Toast.makeText(mContext.getApplicationContext(), "Location permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); } } } 

在你的活动课上:

  public class MainActivity extends AppCompatActivity{ private MarshMallowPermission marshMallowPermission; @Override protected void onCreate(Bundle savedInstanceState) { Log.d("NavHome", "Oncreate_nav"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); marshMallowPermission = new MarshMallowPermission(MainActivity.this); if (!marshMallowPermission.checkPermissionForExternalStorage()) { marshMallowPermission.requestPermissionForExternalStorage(MarshMallowPermission.EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE_BY_LOAD_PROFILE); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case MarshMallowPermission.EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE_BY_LOAD_PROFILE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //permission granted successfully } else { //permission denied } break; } } } 

我使用这个作为基础片段类。 我只要求一个片段的权限,但你可以重构它,并制作一个类似的活动版本。

 public class BaseFragment extends Fragment { private static final int PERMISSION_REQUEST_BLOCK_INTERNAL = 555; private static final String PERMISSION_SHARED_PREFERENCES = "permissions"; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_BLOCK_INTERNAL) { boolean allPermissionsGranted = true; for (int iGranting : grantResults) { if (iGranting != PermissionChecker.PERMISSION_GRANTED) { allPermissionsGranted = false; break; } } if (allPermissionsGranted && permissionBlock != null) { permissionBlock.run(); } permissionBlock = null; } } public void runNowOrAskForPermissionsFirst(String permission, Runnable block) { if (hasPermission(permission)) { block.run(); } else if (!hasPermissionOrWillAsk(permission)) { permissionBlock = block; askForPermission(permission, PERMISSION_REQUEST_BLOCK_INTERNAL); } } public boolean hasPermissionOrWillAsk(String permission) { boolean hasPermission = hasPermission(permission); boolean hasAsked = hasPreviouslyAskedForPermission(permission); boolean shouldExplain = shouldShowRequestPermissionRationale(permission); return hasPermission || (hasAsked && !shouldExplain); } private boolean hasPermission(String permission) { return (ContextCompat.checkSelfPermission(getContext(), permission) == PackageManager.PERMISSION_GRANTED); } private boolean hasPreviouslyAskedForPermission(String permission) { SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_SHARED_PREFERENCES, Context.MODE_PRIVATE); return prefs.getBoolean(permission, false); } private void askForPermission(String permission, int requestCode) { SharedPreferences.Editor editor = getContext().getSharedPreferences(PERMISSION_SHARED_PREFERENCES, Context.MODE_PRIVATE).edit(); editor.putBoolean(permission, true); editor.apply(); requestPermissions(new String[] { permission }, requestCode); } } 

有两个关键的方法你应该使用:

  • hasPermissionOrWillAsk – 使用它来查看是否已经请求了一个权限,并且不想被再次询问的用户拒绝该权限。 当用户给出关于不想要function的最终答案时,这对于禁用UI很有用。

  • runNowOrAskForPermissionsFirst – 使用它来运行一些需要权限的代码。 如果用户已经授予权限,代码将立即运行。 否则,如果用户授予权限,代码将稍后运行。 或者根本不。 这很好,因为你在一个地方指定了代码。

这是一个例子:

 mFragment.runNowOrAskForPermissionsFirst(Manifest.permission.ACCESS_FINE_LOCATION, new Runnable() { @Override public void run() { ...do something if we have permission... } }); 

很高兴得到这个反馈。 这并不是说这个具体的例子有点简单,您还需要检查设备上是否启用了定位服务。 (这与权限不同)另外,它一次只支持一个权限,但是如果你需要一次支持多个权限,那么修改起来很简单。

为了处理运行时权限,Google提供了一个库项目。 你可以从这里查看https://github.com/googlesamples/easypermissions

通过将以下依赖项添加到build.gradle文件来安装EasyPermissions:

 dependencies { compile 'pub.devrel:easypermissions:0.3.0' } 

要开始使用EasyPermissions,请使用Activity(或Fragment)覆盖onRequestPermissionsResult方法:

 public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // Forward results to EasyPermissions EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } @Override public void onPermissionsGranted(int requestCode, List<String> list) { // Some permissions have been granted // ... } @Override public void onPermissionsDenied(int requestCode, List<String> list) { // Some permissions have been denied // ... } } 

在这里你将得到一个工作的例子,这个库如何工作https://github.com/milon87/EasyPermission

这可能是一个更清洁的方式。 将所有权限添加到数组中

 private static final String[] INITIAL_PERMS={ android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION }; private static final int INITIAL_REQUEST=1337; 

无论你的创造方法是什么permision是每个permision

 @RequiresApi(api = Build.VERSION_CODES.M) private boolean canAccessFineLocation() { return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)); } @RequiresApi(api = Build.VERSION_CODES.M) private boolean canAccessCoarseLocation() { return(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)); } @RequiresApi(api = Build.VERSION_CODES.M) private boolean hasPermission(String perm) { return(PackageManager.PERMISSION_GRANTED == checkSelfPermission(perm)); } 

onCreate中调用这个方法

  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if(!canAccessCoarseLocation() || !canAccessFineLocation()){ requestPermissions(INITIAL_PERMS, INITIAL_REQUEST); } } 

现在重写onRequestPermissionsResult

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if(requestCode == INITIAL_REQUEST){ if (canAccessFineLocation() && canAccessCoarseLocation()) { //call your method } else { //show Toast or alert that this permissions is neccessary } } } 

一次有多个权限可以使用。 这工作对我来说,我得到了另一个解决scheme。 如果你给你的targetSdkVersion下面22它适用于我。 它的行为就像从manifest.xml获取权限一样。 testing和为我工作。

 final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; private void insertDummyContactWrapper() { List<String> permissionsNeeded = new ArrayList<String>(); final List<String> permissionsList = new ArrayList<String>(); if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION)) permissionsNeeded.add("GPS"); if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS)) permissionsNeeded.add("Read Contacts"); if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS)) permissionsNeeded.add("Write Contacts"); if (permissionsList.size() > 0) { if (permissionsNeeded.size() > 0) { // Need Rationale String message = "You need to grant access to " + permissionsNeeded.get(0); for (int i = 1; i < permissionsNeeded.size(); i++) message = message + ", " + permissionsNeeded.get(i); showMessageOKCancel(message, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); } }); return; } requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); return; } insertDummyContact(); } private boolean addPermission(List<String> permissionsList, String permission) { if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(permission); // Check for Rationale Option if (!shouldShowRequestPermissionRationale(permission)) return false; } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: { Map<String, Integer> perms = new HashMap<String, Integer>(); // Initial perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED); // Fill with results for (int i = 0; i < permissions.length; i++) perms.put(permissions[i], grantResults[i]); // Check for ACCESS_FINE_LOCATION if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED) { // All Permissions Granted insertDummyContact(); } else { // Permission Denied Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT) .show(); } } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 

更多细节。 检查波纹pipe的链接

https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

有一个好的库可以用来在后台服务需要权限时需要访问权限。 虽然库的一个限制是它不能被用来确定当前是否给予应用权限。 它总是询问用户,如果应用程序还没有他们。

试一试,因为它使生活更简单: Android权限

  if (CommonMethod.isNetworkAvailable(MainActivity.this)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { //showing dialog to select image callFacebook(); Log.e("permission", "granted MarshMallow"); } else { ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}, 1); } } else { Log.e("permission", "Not Required Less than MarshMallow Version"); callFacebook(); } } else { CommonMethod.showAlert("Internet Connectivity Failure", MainActivity.this); } 

我的类请求运行时权限

 public class RequestPermissionHandler { private Activity mActivity; private RequestPermissionListener mRequestPermissionListener; private int mRequestCode; public void requestPermission(Activity activity, @NonNull String[] permissions, int requestCode, RequestPermissionListener listener) { mActivity = activity; mRequestCode = requestCode; mRequestPermissionListener = listener; if (notNeedRequestRuntimePermissions()) { mRequestPermissionListener.onSuccess(); return; } if (isPermissionsGranted(permissions)) { mRequestPermissionListener.onSuccess(); return; } ActivityCompat.requestPermissions(mActivity, permissions, requestCode); } private boolean notNeedRequestRuntimePermissions() { return Build.VERSION.SDK_INT < Build.VERSION_CODES.M; } private boolean isPermissionsGranted(@NonNull String[] permissions) { int result = 0; for (String permission : permissions) { result += ActivityCompat.checkSelfPermission(mActivity, permission); } return result == PackageManager.PERMISSION_GRANTED; } public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == mRequestCode) { if (grantResults.length > 0) { for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { mRequestPermissionListener.onFailed(); return; } } mRequestPermissionListener.onSuccess(); } else { mRequestPermissionListener.onFailed(); } } } public interface RequestPermissionListener { void onSuccess(); void onFailed(); } } 

在类似的Activity使用

 public class MainActivity extends AppCompatActivity { private RequestPermissionHandler mRequestPermissionHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRequestPermissionHandler = new RequestPermissionHandler(); mRequestPermissionHandler.requestPermission(this, new String[] { Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_EXTERNAL_STORAGE }, 123, new RequestPermissionHandler.RequestPermissionListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show(); } @Override public void onFailed() { Toast.makeText(MainActivity.this, "onFailed", Toast.LENGTH_SHORT).show(); } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mRequestPermissionHandler.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 

尝试这个

这是要求在棉花糖版本的权限最简单的方法。

  if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED&&ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { //TO do here if permission is granted by user } else { //ask for permission if user didnot given if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION}, 0); } } 

注意: –不要忘记在清单文件中也添加相同的权限

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

你可以使用我的库 – NoPermission (这只是一个类)

compile 'ru.alexbykov:nopermission:1.1.1'

样品

 PermissionHelper permissionHelper = new PermissionHelper(this); //don't use getActivity in fragment! permissionHelper.check(Manifest.permission.READ_CONTACTS) .onSuccess(this::onSuccess) .onDenied(this::onDenied) .onNeverAskAgain(this::onNeverAskAgain) .run(); 

onRequestPermissionResult:

  @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults); } 

我认为api比Google EasyPermissions更方便。

Interesting Posts