In this tutorial we will see how to send SMS message from one mobile to another mobile. Using this application, you can send an SMS message to a recipient without user intervention. The following Try It Out shows you how...
TRY IT OUT Sending SMS Messages
1. Using Eclipse, create a new Android project and name its as sms.
2. Add the following statements 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" >
<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"
android:onClick="act" />
</LinearLayout>
3. In the AndroidManifest.xml file, add the following statements:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smsv"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".act"
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"/>
</manifest>
4. Add the following statements to the act.java file:
package com.smsv;
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 act extends Activity
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
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();
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getBaseContext(), "message has been sent", Toast.LENGTH_SHORT).show();
}
});
}
}
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 Send SMS button to send an SMS message to the second emulator. The right side emulator shows the SMS message received by the second emulator (note the notification bar at the top of the second emulator.
Categories:
Post a Comment