Uri smsUri = Uri.parse("tel:100861"); Intent intent = new Intent(Intent.ACTION_VIEW, smsUri); intent.putExtra("sms_body", "shenrenkui"); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent);But at least on my phone (Nexus One), this did not work -- at least not fully.
The Message app would start just fine, but it would not pick up the phone number, so you were basically starting from scratch. That defeats the whole purpose of passing the phone number to get you started.
I also saw one example (don't remember where) that provided the URI not in the constructor, as above, but using the setData method, like this:
intent.setData(Uri.parse("tel:" + phoneNumber)); intent.setType("vnd.android-dir/mms-sms");That can work, but you have to be careful. This code won't work either, on any phone, because, as the documentation for setType says, "This method automatically clears any data that was previously set by setData(Uri)." So you have to set the type FIRST.
So on my Nexus One, if you did this, it would actually open the dialer with that phone number. That's not what I wanted.
Eventually, I focused on the URI and discovered that the problem was the scheme. I changed it to "sms" instead of "tel" and it worked!
So here is the final working code:
Uri uri = Uri.parse("sms:" + phoneNumber); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("vnd.android-dir/mms-sms"); intent.setData(uri); startActivity(intent);
No comments:
Post a Comment