前言:有时候在执行一系列流程时,需要去确保定位服务是打开的。这个时候会弹出dialog让我们去设置,如果设置不成功,回来之后点击下一步,还是让你去设置,直到成功为止,才能往下走。
1. 先看效果(布局有点丑)
首先将我们的定位服务开关关闭,点击下一步会出现如下提示:
点击去设置将其服务位置打开:
再返回App后,点击下一步则会显示定位服务已打开:
2. 实现代码
2.1 获取定位服务是否打开的api
private lateinit var locationManager: LocationManager
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
// 位置服务是否打开
val providerEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
2.2 跳转到位置服务设置界面
// REQUEST_LOCATION_SETTING 自己定义请求码 比如:REQUEST_LOCATION_SETTING = 110
startActivityForResult(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),REQUEST_LOCATION_SETTING)
2.2 自定义的dialog
/**
* @param context
* @param title 标题
* @param content 内容
* @param onPositiveButton 点击去设置按钮的回调函数
* @param onNegativeButton 点击取消按钮的回调函数
*/
private fun showSelfTipsDialog(
context: Context,
title: String,
content: String,
onPositiveButton: (() -> Unit)? = null,
onNegativeButton: (() -> Unit)? = null
): AlertDialog {
val dialog = AlertDialog.Builder(context)
.setTitle(title)
.setPositiveButton(R.string.positive_click) { _, _ ->
//未开启,跳转到系统定位页面
startActivityForResult(
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),
REQUEST_LOCATION_SETTING
)
onPositiveButton?.invoke()
}
.setNegativeButton(R.string.negative_click) { _, _ ->
// 点击取消的话,点击不了下一步
onNegativeButton?.invoke()
}
.setCancelable(false)
.create()
dialog.setMessage(content)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
return dialog
}
2.3 从设置页回来后再次检查
/**
* 从设置页面回来,结果回调
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.d(TAG, "onActivityResult...requestCode= $requestCode")
when (requestCode) {
REQUEST_LOCATION_SETTING -> {
val providerEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
if (!providerEnabled) {
Log.d(TAG, "providerEnabled...providerEnabled= $providerEnabled")
showSelfTipsDialog(
context,
getString(R.string.settings_location),
getString(R.string.settings_location_content),
onPositiveButton = {
Log.d(TAG, "go to location setting page. ")
},
onNegativeButton = {
Log.d(TAG, "Hi bro,please make sure the permission passed. ")
}
).show()
}
}
}
}
2.4 最后在onCreate()方法里面调用
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
context = this
setContentView(R.layout.wifi_ap_activity_main)
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
findViewById<Button>(R.id.btn).setOnClickListener {
val providerEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
if (!providerEnabled) {
showSelfTipsDialog(
context,
getString(R.string.settings_location),
getString(R.string.settings_location_content),
onPositiveButton = {
// 知识点:这里是使用了kotlin的高阶函数
// 将这里的所写的函数作为参数传递给调用处
Log.d(TAG, "go to location setting page. ")
},
onNegativeButton = {
Log.d(TAG, "Hi bro,please make sure the permission passed. ")
}
).show()
} else {
Toast.makeText(context, "定位服务已开启,可以进行下一步!", Toast.LENGTH_SHORT).show()
}
}
}
总结
到此就可以实现进行下一步的跳转提示了。
(如果错误,欢迎批评指正,请大佬轻喷)