没有任何第三方库的Android中的多个运行时权限

Learn about adding multiple runtime permissions in this step-by-step tutorial

This article is part of Today I Learned series and was originally posted at my TIL Github Repository and my website at wajahatkarim.com

In my current project at work, I had a task to add run time permissions in an android app whose code is very old and using legacy methods and frameworks/tools. Normally, I use Ted Permissions in all my apps for the runtime permissions and I must say that it’s one hell of an amazing library I ever saw and given the complex scenario and flow of runtime permissions in Android (thanks to Google who always makes sure to make every thing more complicated than ever), this library makes the runtime permissions like a breeze. Wonderful job Ted Park.

ParkSangGwon/TedPermission

Android introduced Runtime Permissions in 一种PI Level 23 (Android Marshmallow 6.0) or later versions. The permissions has a very complicated flow to implement as shown in the below image:

没有任何第三方库的Android中的多个运行时权限

一世mage Credit: Ted Permissions

今天,当我正在寻找一种非常简单的方法来执行此操作时,我震惊地看到Google如何使这一简单的事情变得如此复杂和令人困惑,难以实现,就像他们对Camera API的实现一样。 我花了一整天的时间在代码的开头添加了两个简单的运行时权限。 这是我的操作方式。

重要说明:您不应一次请求所有权限。 而是应仅在应使用该许可时才请求相关许可。 由于我们的代码库很旧而且很复杂,因此我一开始就要求所有权限。

我的应用程式需要存储和位置权限。 因此,将这些权限添加到Android Manifest文件中,如下所示:

没有任何第三方库的Android中的多个运行时权限

现在,应用程序启动后,它会同时请求(位置和存储)权限。 如果两个权限都被授予,则该应用程序将正常执行常规功能。 我们可以在第一个Activity的onCreate()方法中进行操作,如下所示:

没有任何第三方库的Android中的多个运行时权限

请注意,在上面的代码中,我创建了一个String []数组,其中包含我们将在此处请求的所有权限。 现在,让我们实现checkAndRequestPermissions()方法。

没有任何第三方库的Android中的多个运行时权限

如上面的代码所示,我们可以使用ContextCompat.checkSelfPermission()方法检查任何权限的状态。 我们可以使用ActivityCompat.requestPermissions()方法请求任何权限。 这些方法已包含在支持库中。

现在,如果listPermissionsNeeded不为空,则意味着至少一个或多个权限已被拒绝。 因此,requestPermissions()方法将显示权限对话框,并将调用onRequestPermissionsResult()回调方法

没有任何第三方库的Android中的多个运行时权限

现在,这是请求权限后整个案例的处理方式。 第一次,android将正常显示权限。 如果全部接受,则一切正常。 如果任何一个或全部被拒绝,则会显示说明对话框(由开发人员创建),以告诉用户权限为何如此重要。 并且此对话框将允许用户再次授予权限。 现在,android将再次显示权限对话框,但这一次带有一个额外的复选框“不再询问”。

没有任何第三方库的Android中的多个运行时权限

如果通过“不再询问”拒绝了任何许可,则只能从设定值该应用程序。 由于没有这些权限,我的应用无法运行,因此,我将显示另一个对话框,允许用户转到设定值应用程序的屏幕,并允许手动授予权限。

没有任何第三方库的Android中的多个运行时权限

Permissions screen for the Chrome on Android

我们可以使用另一个方法ActivityCompat.shouldShowRequestPermissionRationale(context,Permission)来执行此操作,该方法保留“不再询问”记录的记录。 如果此方法返回true,则表示Android将显示请求权限对话框。 如果为false,则由于用户已选中“不再询问”,因此Android不会显示该对话框。 因此,在这里您必须将用户重定向到“设置”->“权限”屏幕,以允许用户手动授予权限。

现在,让我们看一下onRequestPermissionsResult()方法中的代码。 我也提出了一些意见,因此这应该是不言而喻的。

没有任何第三方库的Android中的多个运行时权限

这是showDialog()方法。 它仅用于创建AlertDialog。

没有任何第三方库的Android中的多个运行时权限

我希望这对您的应用程序开发有所帮助,并解决了运行时权限问题。

w ^ajahat Karim is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on Github Trending. His libraries have about 2000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and Medium to get more updates about his work in Writing, Android and Open Source.

w ^ajahat Karim (@WajahatKarim) | Twitter

Also, if you have any questions you’d like him to answer, contact him through his website at wajahatkarim.com with DEAR WAJAHAT in the subject line.


from: https://dev.to//wajahatkarim/multiple-runtime-permissions-in-android-without-any-third-party-libraries-324l