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<String>(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<String>) {
val needRequestPermissonList = findDeniedPermissions(permissions)
if (null != needRequestPermissonList && needRequestPermissonList.size > 0) {
ActivityCompat.requestPermissions(this,
needRequestPermissonList.toTypedArray(),
PERMISSON_REQUESTCODE)
}
}
/**
* 获取权限集中需要申请权限的列表
*/
fun findDeniedPermissions(permissions: Array<String>): List<String> {
var needRequestPermissonList = ArrayList<String>()
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<String>, 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批量动态申请权限
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...