Over a million developers have joined DZone.

Android SMS Popup - Part Three: The Activity

DZone's Guide to

Android SMS Popup - Part Three: The Activity

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

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:

  1. Get the SMS information from the background
  2. Show that information to the user in a popup window
  3. 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" />

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. */
    public void onCreate(Bundle savedInstanceState) {
        // no need for XML layouts right now
        // we will use a dialog instead

        // 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();
		 msg.setTimestamp( System.currentTimeMillis() );
		 msg.setBody(" this is a test SMS message!");

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);
    	.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
    	AlertDialog alert = builder.create();

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.

Source: Tony's Blog.

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}