Search code examples
androidandroid-intent

Health Connect - programmatically open the app


I followed instructions to add HealthConnect service to the app, however, once the user approves requested permissions, there is no easy way for the user to modify them. They have to go to PlayStore, find HealthConnect, and open it (the icon was recently removed from the launcher).

How does one open health connect with Intent in Android? The following code does NOT work once the permissions are approved requestPermissions.launch(PERMISSIONS).

This is how complete code looks like:

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)
        }
    }

To summarize, when the app has not approved permissions this action takes me to HealthConnect app to approve the permissions. When the app has granted all permission, clicking on the button does nothing. I would like to ALWAYS launch the HealthConnect app.

Thank you


Solution

  • It's now possible in the Android 14 system-based Health Connect to launch directly to the Permissions page for your app, but as far as I can tell it's not possible for the separate app which is required for < 14.

    On 14, you can launch it using the ACTION_MANAGE_HEALTH_PERMISSIONS intent action from HealthConnectManager, passing in your package name.

    You can however launch just the settings using ACTION_HEALTH_CONNECT_SETTINGS on < 14

    Therefore, with these, you can do a best attempt: Show the permissions screen on 14+, and the regular settings otherwise:

    val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
        Intent(HealthConnectManager.ACTION_MANAGE_HEALTH_PERMISSIONS)
            .putExtra(Intent.EXTRA_PACKAGE_NAME, BuildConfig.APPLICATION_ID)
    } else {
        Intent(HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS)
    }
    startActivity(intent)
    

    This assumes that devices running Android 14+ are using the system-based Health Connect, however the HealthConnectClient class is doing this itself - the ACTION_HEALTH_CONNECT_SETTINGS action resolves to the system one on 14, but the legacy one otherwise, so it's safe to say that is the intended behaviour.