Now, we are going to create an app that will respond when an SMS is
received. This app is going to only display the incoming message on a
Toast.
Step 1: Create a New App
For this app, we are going
to create a BroadcastReciever to catch the in coming SMS message.
Though we are not creating an activity for this, generally such apps can
create an activity for the settings page of such apps.
The following is the AndroidManifest.xml for this app:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="recieveSMS.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
package="recieveSMS.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
Step 2: Creating the SMS Receiver
The code for the SMS receiver is as follows:package recieveSMS.com;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class RecieveSMS extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] recievedMsgs = null;
String str = "";
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
recievedMsgs = new SmsMessage[pdus.length];
for (int i=0; i recievedMsgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + recievedMsgs[i].getOriginatingAddress()+ " :" + recievedMsgs[i].getMessageBody().toString();
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
We have to create a class which extends BroadcastReceiver, and we have to override the onRecieve Method.
In
the onRecieve method, we remove the data from the received Intent,
remove the SmsMessage object, and obtain the sender’s address and text
to display on a toast.
Step 3: Running the App
To test
this, you will need two Android emulator instances. We will send the SMS
using an SMS app from one instance to the other. You will be able to
see the Instance number on the top. As seen below, the numbers that I
have are 5554 and 5556.
So from the second Instance, I will send an SMS to the first instance as shown below:
Once the first instance receives an SMS, the information will be displayed as a Toast, as shown below:
SMS is become such an integral and important part of our mobiles
today that there could be numerous different ways in which SMS can be
integrated in your next killer Android app. The usability as well as the
capabilities of you Android apps can be increased tremendously if SMS
is properly integrated.
As seen above, Android as a platform
provides very good support to integrate SMS in your applications. So, go
out there and integrate SMS into you next awesome Android app!
No comments:
Post a Comment