如何在Android Studio中使用意图(Intent)打开WhatsApp或WhatsApp Business?

huangapple go评论95阅读模式
英文:

how to open whatsapp or whatsapp business with intent android studio?

问题

我正在使用这段代码,它可以打开WhatsApp,但无法打开WhatsApp Business,有没有办法验证用户安装了哪个版本的WhatsApp?

String contact = phone;
Double amount_total = pac_val_amo + del_fac_amo;
String total = "S/. " + String.format("%.2f", amount_total);
String message = "Su pedido de " + store + " ha llegado, su pago total es de S/. " + total;
String url = "https://api.whatsapp.com/send?phone=" + contact + "&text=" + message;
try {
    PackageManager pm = getContext().getPackageManager();
    pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
    Toast.makeText(getContext(), "WhatsApp未安装在此设备上。", Toast.LENGTH_SHORT).show();
    e.printStackTrace();
}
英文:

I'm using this code, and it opens WhatsApp, but it doesn't allow me to open WhatsApp business, is there any way to verify which WhatsApp the user has installed?

String contact = phone;
Double amount_total = pac_val_amo + del_fac_amo;
String total = "S/. " + String.format("%.2f", amount_total);
String message = "Su pedido de " + store + " ha llegado, su pago total es de S/. " + total;
String url = "https://api.whatsapp.com/send?phone=" + contact + "&text=" + message;
try {
    PackageManager pm = getContext().getPackageManager();
    pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
           Toast.makeText(getContext(), "WhatsApp is not installed on this Device.", Toast.LENGTH_SHORT).show();
    e.printStackTrace();
    }

答案1

得分: 5

你可以通过使用其包名com.whatsapp.w4b来打开WhatsApp Business。

在开始意图之前,只需在com.whatsapp和com.whatsapp.w4b之间进行if else检查。

英文:

You can open whatsapp business by using its package name com.whatsapp.w4b

Just do an if else check between com.whatsapp and com.whatsapp.w4b before starting the intent

huangapple
  • 本文由 发表于 2020年9月28日 09:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64094974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定