打开特定的 WhatsApp 联系人从我的 Android 应用

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

open specific whatsapp contact from my android app

问题

我想通过点击按钮在我的WhatsApp中打开特定联系人,我正在使用这段代码,但它无法正常工作。

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Uri uri = Uri.parse("smsto:" + "923000000000");
        Intent i = new Intent(Intent.ACTION_SENDTO, uri);
        i.setPackage("com.whatsapp");
        startActivity(i);
    }
});

请指导我。

英文:

I want to open a specific contact in my whatsapp by clicking a button I am using this code but it is not working.

 btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("smsto:"+"923000000000");
            Intent i = new Intent(Intent.ACTION_SENDTO,uri);
            i.setPackage("com.whatsapp");
            startActivity(i);

Please guide me.

答案1

得分: 3

    String contact = "+92 3122804640"; // 使用国家代码和您的电话号码
    String url = "https://api.whatsapp.com/send?phone=" + contact;
    try {
        PackageManager pm = getApplicationContext().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(MainActivity.this, "您的手机未安装WhatsApp应用", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
英文:

Kindly use below code to achieve this.

    String contact = "+92 3122804640"; // use country code with your phone number
        String url = "https://api.whatsapp.com/send?phone=" + contact;
        try {
            PackageManager pm = getApplicationContext().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(MainActivity.this, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

答案2

得分: 1

我正在使用这个:

String phone = "+8801510101010";

PackageManager packageManager = MainActivity.this.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);

try {
    String url = "https://api.whatsapp.com/send?phone=" + phone;
    i.setPackage("com.whatsapp");
    i.setData(Uri.parse(url));
    if (i.resolveActivity(packageManager) != null) {
        MainActivity.this.startActivity(i);
    }
} catch (Exception e){
    e.printStackTrace();
}
英文:

I am Using This:

            String phone = "+8801510101010";

            PackageManager packageManager = MainActivity.this.getPackageManager();
            Intent i = new Intent(Intent.ACTION_VIEW);

            try {
                String url = "https://api.whatsapp.com/send?phone="+ phone;
                i.setPackage("com.whatsapp");
                i.setData(Uri.parse(url));
                if (i.resolveActivity(packageManager) != null) {
                    MainActivity.this.startActivity(i);
                }
            } catch (Exception e){
                e.printStackTrace();
            }

huangapple
  • 本文由 发表于 2020年9月2日 00:56:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63692195.html
匿名

发表评论

匿名网友

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

确定