-2

Is there anyone who knows how to do this in Jetpack Compose? I want to create a function in my jetpack compose app that will open Google Authenticator if the app exists and go to play store if it doesn't exist. I found some answers to this question including using the PackageManager pm = getPackageManager() however, it only applies to java programs. Below is the sample code.

import android.content.pm.PackageManager

private fun isAppInstalled(packageName : String) : Boolean
{
val pm : PackageManager = getActivity().getPackageManager() // i can't access the getPackageManager()
var installed = false
installed = 
try
{
    pm.getPackageInfo(packageName , PackageManager.GET_ACTIVITIES)
    true
}
catch (e : PackageManager.NameNotFoundException)
{
    false
}
return installed
}
nglauber
  • 18,674
  • 6
  • 70
  • 75

1 Answers1

0

I think this is what you need.

fun isAppInstalled(context: Context, packageName: String): Boolean {
    return try {
        context.packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}

If you need to get a Context object from a composable function, you can use LocalContext.current.

@Composable
fun YourComposable() {
    val isInstalled = isAppInstalled(LocalContext.current, "package.that.you.want")
    // ...
}
nglauber
  • 18,674
  • 6
  • 70
  • 75