【问题标题】:Send text message to WhatsApp and SMS using intent create chooser not working anymore使用意图创建选择器向 WhatsApp 和 SMS 发送短信不再起作用
【发布时间】:2019-03-30 13:42:11
【问题描述】:

下面的代码在一周前可以工作。其目的是允许用户选择是否要使用 WhatsApp 或 SMS 发送短信,但现在当我选择 WhatsApp 时,它什么也不做,尽管 SMS 继续工作。

查看正在打印的 logcat:2018-10-25 18:28:28.915 2147-6714/? I/ActivityManager: START u0 {act=android.intent.action.SENDTO dat=smsto:xxxxxxxxxxx flg=0x3000000 cmp=com.whatsapp/.Conversation (has extras)} from uid 10096 即使传递了带有国家/地区代码的有效数字,它也正在打印 smsto:xxxxxxxxxxx

是否有任何可用于此目的的代码或知道此问题?

fun sendMessageToNumber(number: String, text: String) {
    val cleanNumber = number.cleanText()
    val uri = Uri.parse("smsto:$cleanNumber")
    val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
    sendIntent.putExtra("sms_body", text)
    context?.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.fragment_account_chooser_message_title)))
  }

【问题讨论】:

    标签: android sms whatsapp


    【解决方案1】:

    在我的一个项目中,我使用以下代码发送 whatsapp 消息:

    fun sendWhatsappMsg(){
                var toNumber = "+91 xxxxx xxxxx" // contains spaces.
                toNumber = toNumber.replace("+", "").replace(" ", "")
    
                val sendIntent = Intent("android.intent.action.MAIN")
                sendIntent.putExtra("jid", "$toNumber@s.whatsapp.net")
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
                sendIntent.action = Intent.ACTION_SEND
                sendIntent.setPackage("com.whatsapp")
                sendIntent.type = "text/plain"
                startActivity(sendIntent)
    }
    

    为了发送短信,我使用了以下代码:

    fun sendTextMsg(){
               val phone = "xxxxxxxxxx"
               val msg = "smsto:" + phone
               val smsUri = Uri.parse(msg)
               val smsIntent = Intent(Intent.ACTION_SENDTO, smsUri)
               startActivity(smsIntent)
    }
    

    你可以试试这个。两者都为我工作。

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      • 2016-12-12
      相关资源
      最近更新 更多