美文网首页
Android动态获取权限

Android动态获取权限

作者: 会写代码的小猿猴 | 来源:发表于2021-09-07 16:14 被阅读0次

在Android6.0以后很多权限都不能静态获取了,需要动态获取权限,都2021年了,应该没有版本还低于6.0吧,所以动态获取权限基本上是一个安卓程序员必须要掌握的本领了。下面就具体来看一下怎么做吧。
准备工作:新建一个空白的activity

1、在manifest里面添加静态权限

只有添加了静态权限之后才能动态获取权限,这里以常见的四个权限为例:

 <!--存储权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--电话权限-->
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <!--相机权限-->
    <uses-permission android:name="android.permission.CAMERA"/>
    <!--定位权限-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2、在activity中定义一个字符串数组来存放所需权限

 // 要申请的权限
    private val permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE,
        Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION
    )

3、判断每个权限的获取情况,未获取调用requestPermissions获取权限

 val i = ContextCompat.checkSelfPermission(applicationContext, permissions[0])
        val j = ContextCompat.checkSelfPermission(applicationContext, permissions[1])
        val k = ContextCompat.checkSelfPermission(applicationContext, permissions[2])
        val l = ContextCompat.checkSelfPermission(applicationContext, permissions[3])
        if (i != PackageManager.PERMISSION_GRANTED || j != PackageManager.PERMISSION_GRANTED || k != PackageManager.PERMISSION_GRANTED ||
            l != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, permissions, 321)//调用方法获取权限
        }

4、实现权限回调方法

 override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 321) {
            for (i in permissions.indices) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Log.d("zzp", "没有获取权限" + permissions[i])
                } else {
                    Log.d("zzp", "获取权限成功" + permissions[i])
                }
            }
        }
    }

完整activity代码:

class MainActivity : AppCompatActivity() {
    // 要申请的权限
    private val permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE,
        Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val i = ContextCompat.checkSelfPermission(applicationContext, permissions[0])
        val j = ContextCompat.checkSelfPermission(applicationContext, permissions[1])
        val k = ContextCompat.checkSelfPermission(applicationContext, permissions[2])
        val l = ContextCompat.checkSelfPermission(applicationContext, permissions[3])
        if (i != PackageManager.PERMISSION_GRANTED || j != PackageManager.PERMISSION_GRANTED || k != PackageManager.PERMISSION_GRANTED ||
            l != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, permissions, 321)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 321) {
            for (i in permissions.indices) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Log.d("zzp", "没有获取权限" + permissions[i])
                } else {
                    Log.d("zzp", "获取权限成功" + permissions[i])
                }
            }
        }
    }
}

运行结果截图

image.png

下面附上权限列表:
Android各种权限列表

相关文章

网友评论

      本文标题:Android动态获取权限

      本文链接:https://www.haomeiwen.com/subject/swhpwltx.html