Sending SMS using Intent does not add recipients on some devices

给你一囗甜甜゛ 提交于 2019-11-29 07:57:21

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!