Jan 19, 2012

How to Send an SMS on the Android API

,
Many applications developed for mobile phones make use of the Short Message Service, or SMS, to deliver data. The Android Application Interface, or API, supports the SMS and can be used to develop applications that use this technology to send and receive data. To access the Android API, you need to have the Android SDK installed and work in the Eclipse Integrated Development Environment. The ADT Eclipse plug-in is also required to allow the IDE to interface with the Android API.

Instructions

Things You'll Need

  • Eclipse IDE
  • ADT Plug-in for Eclipse
  • Android SDK
    • 1
      Start Eclipse, go to "File" and select "New" to start the new project wizard. Select "Android" as the type of project, enter "SMSMessaging" in the Project Name box, tick "Create New Project in Workspace," enter "net.learn2develop.SMSMessaging" as Package Name, enter "SMS" as Activity Name and "SMS App" as Application Name. Click "Finish" to create the project.
    • 2
      Double-click the "AndroidManifest.xml" file to edit it. Add two permissions to the file by appending this code between the "</Application>" and "</Manifest>" lines:
      <uses-permission android:name="android.permission.SEND_SMS">
      </uses-permission>
      <uses-permission android:name="android.permission.RECEIVE_SMS">
      </uses-permission>
      These permissions allow the user to choose if she wants to install the application.
    • 3
      Expand the "Res" category, open the "Layout" folder and double-click the "main.xml" file to edit it. Delete any code you see in the file and add this code to create a user interface that allows the user to enter the phone number and message text:
      <?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"
      >
      <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Enter the phone number of recipient"
      />
      <EditText
      android:id="@+id/txtPhoneNo"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      />
      <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Message"
      />
      <EditText
      android:id="@+id/txtMessage"
      android:layout_width="fill_parent"
      android:layout_height="150px"
      android:gravity="top"
      />
      <Button
      android:id="@+id/btnSendSMS"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Send SMS"
      />
      </LinearLayout>
    • 4
      Double-click the "SMS.java" file to open and edit it. Delete any code you see in the file and add this code to check if the phone number and message text is entered before the message is sent:
      package net.learn2develop.SMSMessaging;
      import android.app.Activity;
      import android.app.PendingIntent;
      import android.content.Intent;
      import android.os.Bundle;
      import android.telephony.gsm.SmsManager;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.Toast;
      public class SMS extends Activity
      {
      Button btnSendSMS;
      EditText txtPhoneNo;
      EditText txtMessage;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
      txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
      txtMessage = (EditText) findViewById(R.id.txtMessage);
      btnSendSMS.setOnClickListener(new View.OnClickListener()
      {
      public void onClick(View v)
      {
      String phoneNo = txtPhoneNo.getText().toString();
      String message = txtMessage.getText().toString();
      if (phoneNo.length()>0 && message.length()>0)
      sendSMS(phoneNo, message);
      else
      Toast.makeText(getBaseContext(),
      "Please enter both the phone number and the message.",
      Toast.LENGTH_SHORT).show();
      }
      });
      }
      }
    • 5
      Create a new function that sends the message to another device. Append this code to the SMS.java file to create the function:
      public class SMS extends Activity
      {
      private void sendSMS(String phoneNumber, String message)
      {
      PendingIntent pi = PendingIntent.getActivity(this, 0,
      new Intent(this, SMS.class), 0);
      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber, null, message, pi, null);
      }
      }
    • 6
      Press the "F11" key to compile, build and run the application. It will display a nice user interface with two fields, one for the phone number and the other for the text, and a gray "Send SMS" button that is fully functional.

0 commentaires to “How to Send an SMS on the Android API”

Post a Comment

 

HowToYo Copyright © 2011 | Template design by O Pregador | Powered by Blogger Templates