Senior Software Engineer & Architect. I have programmed with a variety of languages. Originally:C/C++, Mostly: Java, Occasionally: PHP, VB, Python, Unix Shell, JavaScript, Mainframe EasyTrieve and JCL. I also do Android development at the present time. Tony is a DZone MVB and is not an employee of DZone and has posted 37 posts at DZone. You can read more from them at their website. View Full User Profile

Android SMS Popup - Part Three: The Activity

04.03.2012
| 6275 views |
  • submit to reddit

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" />
                  </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.

Source: Tony's Blog.

Published at DZone with permission of Tony Siciliani, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)