Как открыть Health Connect с Intent в Android? Следующий код НЕ работает после утверждения разрешений requestPermissions.launch(PERMISSIONS).
Вот как выглядит полный код:
Код: Выделить всё
class HealthConnectFragment: Fragment(R.layout.fragment_health_connect) {
private val analyticsHelper: analyticsHelper by inject()
private var _binding: FragmentHealthConnectBinding? = null
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
// Create the permissions launcher.
private val requestPermissionActivityContract = createRequestPermissionResultContract()
// Create the permissions launcher.
private val requestPermissions =
registerForActivityResult(requestPermissionActivityContract) { granted ->
if (granted.containsAll(HealthConnectApi.PERMISSIONS)) {
Timber.e("1 ALL PERMISSIONS GRANTED")
// Permissions successfully granted
} else {
// Lack of required permissions
Timber.e("1 LACKING PERMISSIONS")
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentHealthConnectBinding.bind(view)
analyticsHelper.logScreenView(
Event.AppSetup.HEALTH_CONNECT_SETUP_FRAGMENT,
HealthConnectFragment::class.simpleName!!)
// Set click listener depending on the permissions.
binding.connectButton.setOnClickListener {
if (HealthConnectClient.isAvailable(requireContext())) {
Timber.e("YAYYA")
launchHealthConnectPermissions()
} else {
// ...
Timber.e("NAYYA")
}
}
}
private fun launchHealthConnectPermissions() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
requestPermissions.launch(healthConnectApi.PERMISSIONS)
}
}
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/740 ... en-the-app
Мобильная версия