Sending SMS using Intent does not add recipients on some devices

后端 未结 1 871
情歌与酒
情歌与酒 2020-12-17 04:35

I send SMS using code below:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(\"smsto:\" + phoneNumber));
        intent.putExtra(\"address\", phoneN         


        
相关标签:
1条回答
  • 2020-12-17 05:30

    I looked into Intent source and it seems that setting intent type removes data and setting data removes type. This is what I've found:

    public Intent setData(Uri data) {
            mData = data;
            mType = null;
            return this;
        }
    
    public Intent setType(String type) {
            mData = null;
            mType = type;
            return this;
        }
    
    public Intent setDataAndType(Uri data, String type) {
            mData = data;
            mType = type;
            return this;
        }
    

    So setting type overrides my data provided in Uri.parse("smsto:" + phoneNumber). I also tried using setDataAndType, but then android just can't find the right Intent to start for such combination... So this is the final solution:

    Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra("address", phoneNumber);
            intent.putExtra("sms_body", messageBody);
            intent.setData(Uri.parse("smsto:" + phoneNumber));
            context.startActivity(intent);
    

    It seems to work on different devices what I can test on. I hope this will be helpful for anyone who faces the same problem.

    Cheers!

    0 讨论(0)
提交回复
热议问题