Introducing Toast

Write App Reviews

Build your own Android App Dev Empire

Get Instant Access

For our example we will create a simple Receiver class that listens for an SMS text message and when a message arrives briefly pops up a message, called a Toast, to the user with the content of the message. A Toast is a simple, nonpersistent message designed to alert the user of some occurring event. Toasts are a great way to let a user know that a call is coming in, an SMS or email has arrived, or some other event hasjust happened.

To look at how we can use a Toast, let's create a simple example. To build the example, first create a new project called SMSNotifyExample in Eclipse. You can use whatever package name you like, but for this chapter we will use Now that we have created the project, let's edit AndroidManifest.xml. You will need to add tags so that your AndroidManifest.xml file looks like listing 8.1.

Listing 8.1 AndroidManifest.xml for SMSNotifyExample

<?xml version= "1.0" encoding= "utf - 8 " ? > Define user permissions to allow SMS messages Q

<manifest xmlns:android="" package="com.msi.manning.chapter8">

<uses-permission android:name="android.permission.RECEIVE_SMS" /> <—

<application android:icon="@drawable/chat"> <-

<activity android:name=".SMSNotifyActivity"

android :label="@string/app_name"> Define a receiver, SMSNotify,

<intent - filter> with an Intent filter ©

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<receiver android:name=".SMSNotifyExample"> <-


<action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter>

</receiver> SMSNotifyExample

</application> acts as receiver Q


The AndroidManifest.xml file needs to have specific user permissions Q added to it to allow incoming SMS messages. The Android security model default is to have no permissions associated with applications, meaning applications can essentially do nothing that might harm the device or the data on the device. To provide Android permission you need to use one or more permissions. In chapter 9 we will go into greater detail about Android's security model.

In the next part Q of the AndroidManifest.xml file we define SMSNotifyActivity, which is simply our Activity, and the next class is the SMSNotifyExample class Q, which will act as our receiver. Then we will create a simple Activity class called SMSNotifyActivity, as in listing 8.2.

Listing 8.2 SMS Activity for the SMSNotifyExample class public class SMSNotifyExampleActivity extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

As you can see there is very little to listing 8.2, in part because for this first example we will be doing little with the Activity. Later in this chapter, we will build on this class. Now let us create our Receiver class (see chapter 5 for more about Intent receivers), which will listen for the SMS message and fire off an action. Listing 8.3 shows the code for our SMSNotifyExample class.

Listing 8.3 A sample SMS IntentReceiver public class SMSNotifyExample extends BroadcastReceiver { <1—i Extend the class as a private static final String LOG_TAG = "SMSReceiver"; O BroadcastReceiver public static final int NOTIFICATION_ID_RECEIVED = 0x1221;

static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; <-|

public void onReceivelntent(Context context, Intent intent) {

if (intent.getAction().equals(SMSNotifyExample.ACTION)) { StringBuilder sb = new StringBuilder();

Action fired by Android

3 when a SMS is received ©

Object [] pdusObj = (Object []) bundle .get ("pdus") ; SmsMessage[] messages = new SmsMessage[pdusObj.length];

for (SmsMessage currentMessage : messages) { <-

sb.append("Received SMS\nFrom: ") ;

sb.append(currentMessage.getDisplayOriginatingAddress()); sb. append ("\n----Message----\n") ;

sb.append(currentMessage.getDisplayMessageBody()); } Build message to

} share to the user D

Log.i(SMSNotifyExample.LOG_TAG, " [SMSApp] onReceivelntent: " + sb) ; Toast.mafceText(context, sb.toString(), Toast.LENGTH_LONG).show();

} Create a Toast

@Override public void onReceive(Context context, Intent intent) {

Listing 8.3 should be very easy to follow. Extend the SMSNotifyExample class using BroadcastReceiver, which allows the class to receive Intent classes O. Then we create a String Q to hold the action that will be fired by the system when an SMS is received. After that we create a simple method to notify the user that an SMS message has been received, and we parse the SMS message to show who it was from and the content of the message ©. Finally we use a Toast to provide a quick message to the user ©.

Toast classes are transient little messages—they pop up and provide the user with quick information without interrupting what the user is doing. In our code we chain two methods together using the form makeText(Context context, CharSquence text, int duration ).show(), where the first method contains a text view for the user and the second method, show(), shows the message to the user. Toast allows you to set a specific view using setView, but for our example we allow it to show the default, which is the Android status bar.

Once you have finished cutting and pasting the code, everything should automatically compile, and you should be able to run the application. The application should come up and look like figure 8.1.

To test our application, select the DDMS option in Eclipse. Now in the Telephony Actions field, type a telephone number, for example, 17035551429. Select SMS and type a message in the Message field; then click Send. Your message should be sent to the emulator, and you should be able to see the emulator responding in the Eclipse console. A message should appear in the Android status bar on the very top of the Android screen representation, as shown in figure 8.2.

So now that we have created our simple example, know how to display a short message upon receiving an SMS, and know how to use the emulator to create an SMS, let's

Figure 8.1 A simple Toast, the SMSNotifyExample, shown running in the emulator

Figure 8.2 Example of a Toast message being generated from an SMS message look at how to create a more persistent message that can also be used to set LEDs, play a sound, or something of that nature, to let the user know an event has occurred.

Was this article helpful?

0 0


Post a comment