【发布时间】:2020-05-21 13:45:05
【问题描述】:
我的应用包含以下代码来检查使用设备位置服务的权限,但我需要某种方法来检测设备中的 Location 是否打开或关闭,如果它关闭则将其打开。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counties)
// selectedCounty = intent.getStringExtra("COUNTY")!!
// Custom action bar code to return to list of counties
// configureCustomActionBar()
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
Log.d("Debug","Permission not granted")
// Permission is not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location needed for navigation", Toast.LENGTH_SHORT).show()
} else {
Log.d("Debug","Request Permission")
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
}
} else {
// Permission has already been granted
Log.d("Debug", "Permission already granted")
}
}
虽然已授予权限,但我仍然需要某种方式打开 Location 或提示用户手动打开它。
【问题讨论】:
-
@greeble31 可以,谢谢。
标签: android kotlin permissions location