How to open Viber public chat from Android app

佐手、 提交于 2019-12-05 17:59:53
Stanislav Tkach

this Kotlin code works fine for me

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

Java code

  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)
                    )
            );
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!