想要在Android应用中打开whatsapp与未保存的联系电话聊天 [英] want to open whatsapp to chat with a unsaved contact number in android app

查看:417
本文介绍了想要在Android应用中打开whatsapp与未保存的联系电话聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开未保存在用户手机中的某个号码的whatsapp聊天框.

i want to open whatsapp chat box for some number which is not saved in user mobile.

我正在使用以下代码:

Uri uri = Uri.parse("smsto:" + str_MobileNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Hello");
i.setPackage("com.whatsapp");
mContext.startActivity(i);

但是whatsapp显示错误:

But whatsapp is showing error :

推荐答案

方法1-使用android组件名称

 public static void openWhatsAppConversation(Context context, String number, String message) {

    number = number.replace(" ", "").replace("+", "");

    Intent sendIntent = new Intent("android.intent.action.MAIN");

    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
    sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number) + "@s.whatsapp.net");

    context.startActivity(sendIntent);
}

方法2-使用whatsapp api uri

public static void openWhatsAppConversationUsingUri(Context context, String numberWithCountryCode, String message) {

    Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + numberWithCountryCode + "&text=" + message);

    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);

    context.startActivity(sendIntent);
}

这篇关于想要在Android应用中打开whatsapp与未保存的联系电话聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆