尝试通过意图直接打开特定数字来启动Whatsapp但失败

时间:2019-01-26 02:09:26

标签: android kotlin

我喜欢启动whatsapp以使用意图将消息发送到某些消息。以下代码成功启动了whatsapp,但未成功启动,我错过了什么吗?

嗨,我喜欢启动whatsapp来使用意图将消息发送到某些消息。以下代码成功启动了whatsapp,但未成功启动,我错过了什么吗?

imageButton_wa.setOnClickListener {
        val packageName="com.whatsapp"
        bukawa(packageName)
    }



private fun bukawa(packageName:String){

    // Get an instance of PackageManager
    val pm = activity!!.baseContext.packageManager

    // Initialize a new Intent
    val iwa:Intent? = pm.getLaunchIntentForPackage(packageName)


    iwa?.data=Uri.parse("https://api.whatsapp.com/send?phone="+62xxxxxxxxx)
     iwa?.action=Intent.ACTION_SENDTO

    activity!!.baseContext.startActivity(iwa)


}

1 个答案:

答案 0 :(得分:0)

这对我有用。请尝试。-

 public static void whatsapp(Activity activity, String phone) {
    String formattedNumber = Util.formatPhone(phone);
    try{
        Intent sendIntent =new Intent("android.intent.action.MAIN");
        sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT,"");
        sendIntent.putExtra("jid", formattedNumber +"@s.whatsapp.net");
        sendIntent.setPackage("com.whatsapp");
        activity.startActivity(sendIntent);
    }
    catch(Exception e)
    {
        Toast.makeText(activity,"Error/n"+ e.toString(),Toast.LENGTH_SHORT).show();
    }
}