Thursday, May 8, 2014
How to Send an SMS on the Android API
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 '
' and '
' lines:
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:
android:orientation='vertical'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
>
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:text='Enter the phone number of recipient'
/>
android:id='@+id/txtPhoneNo'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
/>
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:text='Message'
/>
android:id='@+id/txtMessage'
android:layout_width='fill_parent'
android:layout_height='150px'
android:gravity='top'
/>
android:id='@+id/btnSendSMS'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:text='Send SMS'
/>
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment