SMS Sending in Android

>> Sending SMS in Android :
                                            "SMS messaging is one of the main killer applications on a mobile phone today — for some users as necessary as the phone itself. Any mobile phone you buy today should have at least SMS messaging capabilities, and nearly all users of any age know how to send and receive such messages. Android comes with a built-in SMS application that enables you to send and receive
SMS messages. However, in some cases you might want to integrate SMS capabilities into your own
Android application.
"

For example- you might want to write an application that automatically sends a SMS message at regular time intervals. For example, this would be useful if you wanted to track the location of your kids — simply give them an Android device that sends out an SMS message containing its geographical location every 30 minutes.

This section describes how you can programmatically send and receive SMS messages in your Android
applications. The good news for Android developers is that you don’t need a real device to test SMS
messaging: The free Android Emulator provides that capability.

>> Sending SMS Messages Programmatically : 
                                                                               "You will first learn how to send SMS messages programmatically from within your application. Using this approach, your application can automatically send an SMS message to a recipient without user intervention."the steps of it are as follows.

STEPS:

1. Using Eclipse, create a new Android project and name it as shown in Figure.
           
                                         

2. Add the following statements in bold to the main.xml file:

      <?xml version=”1.0” encoding=”utf-8”?>
         <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
              android:orientation=”vertical”
              android:layout_width=”fill_parent”
            android:layout_height=”fill_parent”
          >
        <Button
           android:id=”@+id/btnSendSMS”
           android:layout_width=”fill_parent”
           android:layout_height=”wrap_content”
           android:text=”Send SMS” />
      </LinearLayout>

3. In the AndroidManifest.xml file, add the following statements in bold:

   <?xml version=”1.0” encoding=”utf-8”?>
      <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
           package=”net.learn2develop.SMS”
           android:versionCode=”1”
          android:versionName=”1.0”>
      <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
      <activity android:name=”.MainActivity”
       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-sdk android:minSdkVersion=”8” />
     <uses-permission android:name=”android.permission.SEND_SMS”></uses-permission>
  </manifest>

4. Add the following statements in bold to the MainActivity.java file:

   package net.learn2develop.SMS;
   import android.app.Activity;
   import android.os.Bundle;
   import android.app.PendingIntent;
   import android.content.Intent;
   import android.telephony.SmsManager;
   import android.view.View;
   import android.widget.Button;
   public class MainActivity extends Activity {
   Button btnSendSMS;
         public void onCreate(Bundle savedInstanceState) {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.main);
                     btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
                     btnSendSMS.setOnClickListener(new View.OnClickListener()
                    {
                          public void onClick(View v)
                         {
                              sendSMS(“5556”, “Hello my friends!”);
                          }
                     });
           }
          //*** sends an SMS message to another device ***
           private void sendSMS(String phoneNumber, String message)
          {
                   SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(phoneNumber, null, message, null, null);
           }
   }

5. Press F11 to debug the application on the Android Emulator. Using the Android SDK and AVD
Manager, launch another AVD.

6. On the first Android Emulator, click the Send SMS button to send an SMS message to the second
emulator. The left side of Figure 8-2 shows the SMS message received by the second emulator (note
the notification bar at the top of the second emulator).