kotlin:安卓开发动态申请多个权限
发表于更新于
广州
开发androidkotlin:安卓开发动态申请多个权限
❄️2winter应用接入支付宝的sdk,需要申请一些权限。
这里是kotlin的代码:
对了:6.0以后有3组,27个权限是需要动态申请的,具体,百度吧。
下面申请了两个权限(要在manifest文件注册哈)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| val permissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE) val mPermissionList = ArrayList<String>() val mRequestCode = 0x1 fun initPermission() { mPermissionList.clear() for (permission in permissions) { if (ContextCompat.checkSelfPermission( this@MainActivity, permission ) != PackageManager.PERMISSION_GRANTED ) { mPermissionList.add(permission) } } if (!mPermissionList.isEmpty()) { ActivityCompat.requestPermissions(this@MainActivity, permissions, mRequestCode)
} else { Toast.makeText(this@MainActivity,"全部授予!",Toast.LENGTH_SHORT).show() } }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { 0x1 -> for (i in 0 until grantResults.size) { if (grantResults[i] != PackageManager.PERMISSION_GRANTED) Toast.makeText(this,"您有未授予的权限,可能影响使用",Toast.LENGTH_SHORT).show() }
}
|
❄️2winter
ReactNative FullStack Developer
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ❄️2winter!