封装了一个关于权限相关和开关的申请Demo,先贴出来视频效果,最开始的时候GPS开关没打开,没有位置权限,没开启蓝牙功能,点击按钮之后先后打开GPS开关,动态申请位置权限,打开GPS开关和蓝牙功能。
贴出核心代码:
1. 首先先封装了一个动态权限请求的工具类,蓝牙功能的申请也写在其中:
class PermissionsUtils {
companion object {
fun checkGPS(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
@RequiresApi(Build.VERSION_CODES.M)
fun checkLocationPermission(activity: Activity): Boolean {
return ContextCompat.checkSelfPermission(
activity,
Manifest.permissi