Android kotlin批量动态申请权限


import android.Manifest
import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.support.v4.app.ActivityCompat
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
import android.widget.Toast


// Created by CIDI daiqinxue on 2018/11/13.
abstract class BaseActivity : Activity() {
    /**
     * 需要进行检测的权限数组
     */
    val needPermissions = arrayOf(Manifest.permission.RECEIVE_BOOT_COMPLETED,Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_EXTERNAL_STORAGE)
    /**
     * 判断是否需要检测,防止不停的弹框
     */
    val PERMISSON_REQUESTCODE = 110
    val LOCATION_CODE = 1315
    var isNeedCheck = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
        if (isNeedCheck) {
            checkPermissions(needPermissions);
        }
    }

    fun checkPermissions(permissions: Array) {
        val needRequestPermissonList = findDeniedPermissions(permissions)
        if (null != needRequestPermissonList && needRequestPermissonList.size > 0) {
            ActivityCompat.requestPermissions(this,
                    needRequestPermissonList.toTypedArray(),
                    PERMISSON_REQUESTCODE)
        }
    }

    /**
     * 获取权限集中需要申请权限的列表
     */
    fun findDeniedPermissions(permissions: Array): List {
        var needRequestPermissonList = ArrayList()
        for (perm in permissions) {
            if (ContextCompat.checkSelfPermission(this,
                            perm) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale(
                            this, perm)) {
                needRequestPermissonList.add(perm)
            }
        }
        return needRequestPermissonList
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, paramArrayOfInt: IntArray) {
        when (requestCode) {
            PERMISSON_REQUESTCODE -> if (!verifyPermissions(paramArrayOfInt)) {
                isNeedCheck = false
            }
            LOCATION_CODE -> {
                if (paramArrayOfInt.size > 0 && paramArrayOfInt[0] == PackageManager.PERMISSION_GRANTED) {
                    // 权限被用户同意。

                } else {
                    // 权限被用户拒绝了。
                    Toast.makeText(this@BaseActivity, "定位权限被禁止,相关地图功能无法使用!", Toast.LENGTH_LONG).show()
                }

            }
        }

    }


    /**
     * 检测是否说有的权限都已经授权
     */
    fun verifyPermissions(grantResults: IntArray): Boolean {
        for (result in grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false
            }
        }
        return true
    }

}

你可能感兴趣的:(Android kotlin批量动态申请权限)