In Part One, we captured SMS messages using a BroadcastReceiver. In Part Two, among a set of options, we chose to pass the needed SMS information (sender, message and timestamp) as a Serializable ‘PopMessage’ object from the background to the foreground. In this section, we will construct the screen to show to our users.
What do we want? Three things basically:
- Get the SMS information from the background
- Show that information to the user in a popup window
- Allow the user to react to that message within the application, either by dismissing it, or by responding to it by using his/her phone’s favorite SMS program a button click away.
By now, you must have read the Android developer guide section on Activities. So let’s start coding our PopSMSActivity class which will provide our UI. For now, that class will be our only and main Activity, declared in our Android Manifest:
<application ...>
<activity android:label="@string/app_name"
android:name=".PopSMSActivity" ...>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
We declare a single Activity, the PopSMSActivity, as the main entry point to our little application. For our development purposes, we can launch our application anytime and see how the popup looks like, without having to wait for an SMS to arrive. For the UI, there are several kinds of layout to choose from, and that is a lot of XML. What we need right now is something as simple as possible, so we will program dynamically an Android alert dialog.
When we will start building more features, we will have to use a more sophisticated layout UI and replace the main launcher by a settings screen where we would do all kinds of customization, like choosing colors, sound effects, having a popup only for specific ‘VIP’ phone numbers etc… and even have a “Preview” button to see how our popup window will look like before use, instead of launching the Dialog directly as we will do now. But for now, here’s the Activity class:
public class PopSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no need for XML layouts right now
// we will use a dialog instead
//setContentView(R.layout.main);
// retrieve Serializable sms message object
// by the key "msg" used to pass it
Intent in = this.getIntent();
PopMessage msg = (PopMessage) in.getSerializableExtra("msg");
// Case where we launch the app to test the UI
// i.e. no incoming SMS
if(msg == null){
msg = new PopMessage();
con.setPhone("0123456789");
msg.setTimestamp( System.currentTimeMillis() );
msg.setBody(" this is a test SMS message!");
}
showDialog(msg);
}
The ShowDialog() method creates a very basic UI, i.e. a typical Android dialog with a text and two buttons:
/***/
private void showDialog(PopMessage msg){
final String sender = msg.getPhone();
final String body = msg.getBody();
final String display = sender + "\n"
+ msg.getShortDate( msg.getTimestamp() )+ "\n"
+ body + "\n";
// Display in Alert Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(display)
.setCancelable(false)
.setPositiveButton("Reply", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// reply by calling SMS program
smsReply(sender, body);
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// go back to the phone home screen
goHome();
}
});
AlertDialog alert = builder.create();
alert.show();
}
This is how it looks like on the phone’s screen:
![]() |
What is left in order to have a very basic working application, is handling those two button clicks, and talk a bit about Implicit Intents. In Part Four.
This article is also available at JavaLobby.

#1 by DvTonder on April 4, 2012 - 3:15 pm
part 4 please
#2 by Saket on April 9, 2012 - 12:29 am
Very helpful tutorial and it is helping me on getting started with Android dev.