Friday, January 16, 2015

Send sms from Android application

This tutorial demonstrate how we can implement a simple Android application which can send SMS. To check this application I used two emulators. If you are planning to  use emulator to check this you need to create two emulators first. I have tested this using Android 2.2 two emulators.


1. First create two emulators
    One should be emulator-5554 and the other one should be emulator-5556

2. Here is the Activity code for send SMS using intent.

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class TestSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String number = "5556";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
}
}

The variable "number" in line 11 is the the recipient of the SMS activity. Im going test this application using two emulators therefore I used one emulator number for that variable. If you are going to send SMS to mobile phone replace number with mobile phone number, but remember that sending SMS from emulator to a cell phone is difficult.

Dont forget to add send SMS permission to your Manifest file.


3. Run the project.
Android device chooser window will appear. Choose an AVD. If you assigned 5556 to number variable then choose emulator-5554 to run program, other vice select emulator-5554.



Then compose message window will open. Type a simple text (Eg:-"Hello world!") there and click on send.

emulator-5554


You can see received SMS on the other emulator

emulator-5556

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.