You might need to use your app to send messages to other people. This can be implemented in two ways:
1. By the use of SmsManager.
2. By the use of Buit-in Intent.
Using SmsManager
It manages the operations involving the sending of messages to other mobile phones. It is implemented as follows:
SmsManager manager = SmsManager.getDefault();
We have implemented it by calling the method SmsManager.getDefault() ,which is a static method. The instantiated object above, that is, “manager” can then be used to send
messages to the specified mobile number as shown below:
manager.sendTextMessage(“phoneNo”, null, “SMS”, null, null);
Other methods which can be applied to the class SmsManager include the following:
1. ArrayList<String> messagedivision (String text)- a single message is divided
into several fragments. No fragment is bigger than the original SMS message.
2. static SmsManager getDefault()
Returns to the user the default instance of SmsManager.
For you to demonstrate sending a Sms text, you need a physical device running android.
otherwise, the emulator might end up failing. You should also target SDK.
Create a new project and give it a name of your choice. Modify the activity_main.xml file to the following:
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical” >
<TextView
android:id=”@+id/mobNo”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Enter Phone Number: ” />
<EditText
android:id=”@+id/editmobNo”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:inputType=“phone”/>
<TextView
android:id=”@+id/tvMessage”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Enter SMS Message” />
<EditText
android:id=”@+id/edSMS”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:inputType=“textMultiLine”/>
<Button android:id=”@+id/btnsendsms”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“Send SMS “/>
</LinearLayout>
Modify the java class MainActivity,java to the following:
import android.telephony.SmsManager;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity { Button sbtn;
EditText mobNo;
EditText msge;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbtn = (Button) findViewById(R.id.btnsendsms);
mobNo = (EditText) findViewById(R.id.editmobNo);
msge = (EditText) findViewById(R.id.edSMS);
sbtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
sendMessage();
} });
}
protected void sendMessage() { Log.i(“Send SMS”, ””);
String mobNo = mobNo.getText().toString();
String message = msge.getText().toString();
try {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(mobNo, null, message, null, null);
Toast.makeText(getApplicationContext(), “SMS sent.”, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
“sending failed, please try again.”, Toast.LENGTH_LONG).show();
e.printStackTrace();
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
The AndroidManifest.xml file should be modified to the following:
<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.example.sendsmstext”
android:versionCode=“1”
android:versionName=“1.0” >
<uses-sdk
android:minSdkVersion=“8”
android:targetSdkVersion=“17” />
<uses-permission android:name=“android.permission.SEND_SMS” />
<application
android:allowBackup=“true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=“com.example.sendsmstext.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>
</manifest>
Now that you are finished, it’s time to run the app. Just connect the device to the computer using a USB cable. Select the project or open one of its files and click “Run” from the toolbar. Select to run the app on the device. The following will be seen:
Make sure that your GSM connection is okay. Enter the phone number of the recipient, followed by the SMS message itself. Finally, click on the “SendSMS” button. Your message will be delivered.
Using Built-in Intent
In this method, ACTION_VIEW is used to launch the SMS client installed on your device.
Intent intent = new Intent(Intent.ACTION_VIEW);
Smsto: and the type of data to be send should also be specified:
intent.setData(Uri.parse(“smsto:”));
intent.setType(“vnd.android-dir/mms-sms”);
You can then specify both the phone number and message as follows:
intent.putExtra(“address” , new String(“1123406889;3493694320”));
intent.putExtra(“sms_body” , “SMS to Mike”);
Note that in the above example, we are sending the SMS to two numbers and we have used a semi-colon (;) to separate the two. Both the “address” and the “sms_body” should be written in lower case, since they are case sensitive.
Example:
Create a new project and give it a name of your choice. Modify the activity_main.xml file to the following:
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical” >
<Button android:id=”@+id/smssend”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“Compose SMS”/>
</LinearLayout>
Modify the MainActivity.xml to the following:
import android.app.Activity;
import android.net.Uri;
import android.util.Log;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sbtn = (Button) findViewById(R.id.smssend);
sbtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
sendSMSText();
} });
}
protected void sendSMSText() { Log.i(“Send SMS “, ””);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“smsto:”));
smsIntent.setType(“vnd.android-dir/mms-sms”);
intent.putExtra(“address” , new String (“7825456929”));
intent.putExtra(“sms_body” , “Send SMS to Mike”);
try {
startActivity(intent);
finish();
Log.i(“SMS has been sent…”, ””);
} catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this,
“sending SMS has failed, try again.”, Toast.LENGTH_SHORT).show();
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) { // items are added to the action bar if they are available.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
The NadroidManifest.xml file should be as follows:
<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.example.sendsmstext”
android:versionCode=“1”
android:versionName=“1.0” >
<uses-sdk
android:minSdkVersion=“8”
android:targetSdkVersion=“17” />
<application
android:allowBackup=“true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=“com.example.sendsmstext.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>
</manifest>
Once you have finished, connect your phone to the computer, select the project and click
“Run”. Choose to run on the device. The following screen will appear:
Click on the “Compose SMS” button. You can modify the defaults that will appear. Once you write the message and the phone number, click on send and the SMS will be sent.