Pages

Sunday 14 October 2012

How to Handle SMS in Android Development

SMS is an integral part of mobile devices and mobile applications. An overwhelming majority of mobile users use the SMS service on their mobiles; some use it dozens of times per day. Android provides a very good API so that developers can integrate SMS technology into their apps, increasing the utility and appeal of their applications. In this article we are going to examine several apps that use SMS technology via the APIs provided by Android.
 

How to Launch the SMS Application From Your Program.

We are now going to create a small app which will say “hello” to our friends by sending an SMS message. This app will take the friend’s number from the user, then the application will launch the SMS application of the mobile phones with the number the user entered, and finally it will send a “hello” message.

Step 1: Creating the UI and layout

First, create a simple activity called as LaunchSMS , then we will create the lay out as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="4dip"
android:text="Recipient Number"
/>
<EditText android:id="@+id/messageNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
<Button android:id="@+id/sayhello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Say Hello!"
android:onClick="sayHello"
/>
</LinearLayout>

Above, we have created a linear layout with one TextView to tell the user that he has to enter the recipient’s number. Then, we have one EditText to take in the number from the user, and one button which, when clicked, will call the method sayHello.

The code for the activity is as follows:

package launchSMS.com;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class LaunchSMS extends Activity {
    /** Called when the activity is first created. */
    private EditText messageNumber;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        messageNumber=(EditText)findViewById(R.id.messageNumber);
    }
    public void sayHello(View v) {
        String _messageNumber=messageNumber.getText().toString();
        String messageText = "Hi , Just SMSed to say hello";
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setData(Uri.parse("sms:"+_messageNumber));
        sendIntent.putExtra("sms_body", messageText);
        startActivity(sendIntent);
    }
}

Step 2: Initializing the Activity

Using the onCreate method, we will set main layout we created as the content view. Then, I have created a private member to hold the EditBox for the message number.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        messageNumber=(EditText)findViewById(R.id.messageNumber);
    }

The UI for the app will look as follows:



Step 3: Launching the SMS App

public void sayHello(View v) {
		String _messageNumber=messageNumber.getText().toString();
		String messageText = "Hi , Just SMSed to say hello";
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		sendIntent.setData(Uri.parse("sms:"+_messageNumber));
        sendIntent.putExtra("sms_body", messageText);
        startActivity(sendIntent);
	}
 
In the function sayHello, we first get the number which the user has entered in the EditText. Then, we create a String variable to hold the message text that we want to send.

Now, to launch the SMS application, we have to create the following:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);





Then, we set the intent data with the number entered by the user as follows:

sendIntent.setData(Uri.parse("sms:"+_messageNumber));

Finally, the message text goes as extra data in the intent using the method putExtra on the intent.

sendIntent.putExtra("sms_body", messageText);

Then, the intent is sent by passing the created intent to startActivity. This will launch the SMS application of the user with the number and message text already prepopulated. The user can make edits if he or she wants, and then they will just have to press “send” to send the message to his or her friend.

So, using the intents, we can launch the SMS app from within our program. Enjoy ...

1 comment: