简体   繁体   English

识别应用程序是否存在,如果不存在 go 在 Jetpack Compose 中播放商店

[英]Identifying if an app exists, if not go to play store in Jetpack Compose

Is there anyone who knows how to do this in Jetpack Compose?有没有人知道如何在 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.我想在我的jetpack compose应用程序中创建一个function,如果该应用程序存在,它将打开Google Authenticator,如果它不存在,则打开go来播放商店。 I found some answers to this question including using the PackageManager pm = getPackageManager() however, it only applies to java programs.我找到了这个问题的一些答案,包括使用 PackageManager pm = getPackageManager() 但是,它仅适用于 java 程序。 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
}

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 .如果您需要从可组合的 function 中获取Context object,则可以使用LocalContext.current

@Composable
fun YourComposable() {
    val isInstalled = isAppInstalled(LocalContext.current, "package.that.you.want")
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM