Pages

Sunday 14 October 2012

How to Send SMS Directly Via the API Provided by Android

Now, we are going to create an app where DirectSendSMS will be the enhanced version of the previous app. This app will directly send the “hello” message to the user using the Android SMS API.

Step 1: Creating the UI and Layout

First we need to create a new activity, DirectSendSMS. The UI of this app is going to be the same as the one described above, so for this also we will create a linear lay out and add one Textiew, one EditText, and one button.
The layout is 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>
 

Step 2: Initializing the Activity

The initializing of the activity is also same as described in the previous app. Within the onCreate function, we set the main layout as the content view and take the EditBox in a private variable.

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

Step 3: Specifying the Permission for Sending the SMS

In Android, one has to specify all of the permissions needed by the app in the AndroidManifest.xml. By doing so while installing the app, all the permissions required by the app will be shown to the user.
For the ability in our app to send messages, we need to add the android.permission.SEND_SMS permission into AndroidManifest.xml as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="directSendSMS.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DirectSendSMS"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
</manifest>
 

Step 4: Sending the SMS

public void sayHello(View v) {
    String _messageNumber=messageNumber.getText().toString();
    String messageText = "Hi , Just SMSed to say hello";
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(_messageNumber, null, messageText, null, null);
}

In the sayHello function, we get the number that the user entered. In a variable, we hold the message text that we want to send. Then, we get the SmsManager object as follows:

SmsManager sms = SmsManager.getDefault();


Then, using the sendTextMessage method of SmsManager, we send the message.

 

Step 5: Displaying a Toast When the Message is Successfully Sent

public void sayHello(View v) {
        String _messageNumber=messageNumber.getText().toString();
        String messageText = "Hi , Just SMSed to say hello";
        String sent = "SMS_SENT";
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(sent), 0);
        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                if(getResultCode() == Activity.RESULT_OK)
                {
                  Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(getBaseContext(), "SMS could not sent",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, new IntentFilter(sent));
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(_messageNumber, null, messageText, sentPI, null);
    }

Now, we have enhanced the sayHello method to display a Toast upon successfully sending the message. We create a new PendingIntent for this and pass it as an argument to the sendTextMessage method. We also register a receiver for this intent, which checks the result code and displays a Toast saying SMS sent.

Now, the app will look like the following:

No comments:

Post a Comment