Jetpack Compose 中的共享表

问题描述 投票:0回答:2

如何在 Jetpack Compose 中使用 Android 的共享表共享内容?

android kotlin android-jetpack android-jetpack-compose
2个回答
30
投票

@Composable
内,您可以以
标准
方式使用Intent

类似:

val sendIntent: Intent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
    type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
val context = LocalContext.current

Button(onClick = {
    context.startActivity(shareIntent)
}){
    Text("Share")
}


0
投票

Jetpack compose Sharesheet 中,您可以通过在任何可组合函数中传递 context 来调用以下函数

    fun shareAppPlayStoreUrl(context: Context){
    val sendIntent: Intent = Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_TEXT, "Check out this awesome app\n "+"https://play.google.com/store/apps/details?id="+context.applicationInfo.packageName)
        type = "text/plain"
    }
    val shareIntent = Intent.createChooser(sendIntent, null)
    context.startActivity(shareIntent)
}
© www.soinside.com 2019 - 2024. All rights reserved.