Код: Выделить всё
//in AndroidManifest
//In MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var locationManager: LocationManager
...
private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show()
} else {
// Permission is denied, handle the case appropriately
Toast.makeText(this, "permission denied", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Example of how to trigger the permission request
if (!hasSmsPermission2(this)) {
requestPermissionLauncher.launch(android.Manifest.permission.ACCESS_FINE_LOCATION)
} else {
Toast.makeText(this, "ACCESS_FINE_LOCATION already granted", Toast.LENGTH_SHORT).show()
}
// Example of how to trigger the permission request
if (!hasSmsPermission3(this)) {
requestPermissionLauncher.launch(android.Manifest.permission.ACCESS_COARSE_LOCATION)
} else {
Toast.makeText(this, "ACCESS_COARSE_LOCATION already granted", Toast.LENGTH_SHORT).show()
}
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0f, this)
}
override fun onLocationChanged(location: Location) {
val latStr= location.latitude.toString()
val longStr = location.longitude.toString()
}
}
Я понимаю, что между версиями студии Android будут различия, но не полностью скомпилировать. Любая помощь будет оценена по достоинству.
Подробнее здесь: https://stackoverflow.com/questions/798 ... e-latest-a
Мобильная версия