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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…