Код: Выделить всё
//in AndroidManifest
//In MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var locationManager: LocationManager
...
private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
// Permission is granted, proceed with sending SMS
Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show()
// Call your function to send SMS here
} 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 {
// Permission already granted, proceed with SMS functionality
Toast.makeText(this, "READ_SMS 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 {
// Permission already granted, proceed with SMS functionality
Toast.makeText(this, "RECEIVE_SMS 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
Мобильная версия