Android动态权限申请 Kotlin实现

作者: Shanyaliux | 来源:发表于2020-04-13 08:20 被阅读0次

    原文链接https://www.shanya.world/archives/3231ae8c.html

    首先还是先去 AndroidManifest.xml文件中写上需要的权限,这里以存储权限为例。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.shanya.permissionsdemo">
    
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    之后在代码中进行动态申请,这里使用一个Button触发
    MainActivity.kt

    package com.shanya.permissionsdemo
    
    import android.Manifest
    import android.content.pm.PackageManager
    import android.os.Build
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import androidx.core.content.ContextCompat
    import kotlinx.android.synthetic.main.activity_main.*
    
    const val REQUEST_WRITE_EXTERNAL_STORAGE = 1
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            button.setOnClickListener {
                if (ContextCompat.checkSelfPermission(this,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                    requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                            REQUEST_WRITE_EXTERNAL_STORAGE)
                }else{
                    Toast.makeText(this,"已申请权限",Toast.LENGTH_SHORT).show()
                }
            }
        }
    
        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
            when(requestCode){
                REQUEST_WRITE_EXTERNAL_STORAGE -> {
                    if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(this,"已申请权限",Toast.LENGTH_SHORT).show()
                    }else{
                        Toast.makeText(this,"申请权限失败",Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Android动态权限申请 Kotlin实现

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