我知道如何用Viber中的号码打开对话: How to start Viber call from an Android app [new version]?
但是如何打开公开聊天?有什么想法吗?
提前致谢
答案 0 :(得分:0)
此Kotlin代码对我来说很好
val viberPackageName = "com.viber.voip"
val phone= "5757575757"
try {
activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("viber://add?number=$phone")))
} catch (ex: ActivityNotFoundException) {
try {
activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$viberPackageName")))
} catch (ex: ActivityNotFoundException) {
activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$viberPackageName")))
}
}
答案 1 :(得分:0)
Java代码
public void addViberNumber(Context context,String phone) {
String viberPackageName = "com.viber.voip";
try {
context.startActivity(new
Intent(Intent.ACTION_VIEW,
Uri.parse("viber://add?number="+phone)
)
);
} catch (ActivityNotFoundException ex) {
try {
context.startActivity
(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=+" + viberPackageName))
);
} catch (ActivityNotFoundException exe) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + viberPackageName)
)
);
}
}
}
答案 2 :(得分:0)
我使用com.google.i18n.phonenumbers.PhoneNumber
作为传递它的模型,但是功能相同。
对于Viber,您需要将countryCode和nationalNumber作为String格式,然后将其通过Viber指定的Intent URI传递到Uri
中。
然后,您只需启动Intent。
private fun launchViberChat(phoneNumber: Phonenumber.PhoneNumber) {
val formatString = "${phoneNumber.countryCode}${phoneNumber.nationalNumber}"
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("viber://add?number=$formatString")
).apply {
setPackage("com.viber.voip")
}
startActivity(intent)
}