Hey everyone,
So in this post, I thought I would show you guys two basic examples for how to deal with sending SMS messages from within your application. Also, I’ll point out some of the difficulties that I learned, which weren’t documented.
Method #1
SmsManager sm = SmsManager.getDefault(); // here is where the destination of the text should go String number = "6508570720"; sm.sendTextMessage(number, null, "Test SMS Message", null, null);Now, note that this is the most BASIC way to deal with this. You’ll notice that several of the input parameters are null and that’s because you can send optional Pending Intents with the text message to keep track of things such as whether or not the message sent was successful, etc. (For more on this, a good site to conduct would be Advanced SMS Example.)
To test this, one can open two instances of your emulator (one will be port 5554 and one will start on port 5556) and letting the destination number be the port number (i.e. String number = “5556″) will simulate the sending of the text message.
Now, one little thing to note which I figured out but which I don’t think is documented, is that your message MUST be less than or equal to 160 characters. If it’s greater than 160 characters, the method sendTextMessage will throw a Null Pointer Exception.
This method will allow you to immediately send a text with your predetermined message, but let’s say you want to just open the default SMS screen and you want to allow the user to type their own text message, then you can:
Method #2
public class SendSMSActivity extends Activity { @Override public void onCreate(Bundle savedInstance) { // other stuff // the destination number String number = "6508570720"; startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null))); } }And this will bring up the default SMS sending screen.
Now, one last thing to mention – the permissions. For Method #1, in order for it to work you’re going to need to add the permission
However, for Method #2, you don’t need to add any additional permissions as it is simply an ACTION_VIEW intent.
So, between this post and the Advanced SMS Tutorial linked above, I hope this solves all of your SMS sending problems!
Happy coding.
- jwei
[Via http://thinkandroid.wordpress.com]
No comments:
Post a Comment