Android SMS popup – Part Four: Implicit Intents

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 alert dialog that we constructed in Part Three:

In this last section, we will complete this basic application by handling the user actions through button clicks. There are two actions the user may perform:

  1. Close the SMS popup window once the message is read
  2. Choose to respond to it using his favorite SMS program.

We used Intents in the previous parts of this series, as asynchronous messages to pass data between components:

    // In our BroadcastReceiver class, we're passing the
    // SMS message pop_msg to the PopSMSActivity, i.e. our UI.
     intent.setClass(context, PopSMSActivity.class);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     intent.putExtra("msg", pop_msg);
     context.startService(intent);

The above code uses an Explicit Intent, i.e an Intent that indicates a particular class (PopSMSActivity) to pass data to. It is basically a direct call to another component (Service, Activity…). But Intents can also be used to send messages to the Android system so that the latter can determine what course of action to take.

Implicit  Intents do not designate a specific class which should be called by the system, but  the action which we would like to be performed by the system. How does Android know which component(s) to call in order to perform that action? Because those applications/components that can handle the action have previously registered themselves in the system. But how did they do that? The same way we did with our custom SMS receiver in our application’s Manifest in Part One of this series:

<!-- Incoming SMS messages can be intercepted by the SMSReceiver class -->
<receiver android:name="com.ts.pop.sms.SMSReceiver">
  <intent-filter android:priority="999" android:exported="true">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter>
</receiver>

By using an intent-filter, we indicated to the Android system that our application was a candidate for handling the SMS_RECEIVED event. Intent filters are how components declare their capabilities so that other components can use them. Android will look at the action, data, and category of the Intent as part of its Intent Resolution process.

A given component can declare any number of Intent filters, corresponding to the number of actions it can potentially handle. If a component does not have intent-filters, it can only respond to explicit intents. When there are several components that have the same Intent filters, Android will present the user with a list to choose from.

What the smsReply() method below is doing, is asking Android to bring up any MMS-SMS program it can find on the phone :

    /***/
    private void smsReply(String sender, String body){
    	Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    	sendIntent.putExtra("address", sender);
    	sendIntent.putExtra("sms_body", body);
    	sendIntent.setType("vnd.android-dir/mms-sms");
    	startActivity(sendIntent);
    	this.finish(); // close this Activity now
    }

This is what it looks like on the phone, once the “Reply” button is clicked and the SMS program (second screen) is brought up automatically to send a response:

The goHome(), sweet home method called on closing the dialog, simply takes the user back to the phone Home screen :

    /***/
    private void goHome(){
    	Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        this.finish();
    }

That’s it. We now have our first working version of an SMS popup application, and we can start building whatever new features we might think of on top of it.
Here’s one example of what can be done, by adding the list of phone contacts, sounds, and a settings screen.

This article is also available at JavaLobby.

About these ads

, , ,

  1. #1 by nivrad on October 4, 2012 - 11:21 am

    Its not working. i have a force close error.

    • #2 by Tony S. on October 4, 2012 - 12:21 pm

      You’re not likely to get any help when the only information is “it’s not working” or “it crashed”.

      Use a debugger and find out what’s wrong with your code.

  2. #3 by arrulgerby on November 5, 2012 - 8:05 am

    my i ask for source code examples from this project
    for learning because i try very hard from the beginning a lot of errors programm
    my email arulbardai@gmail.com
    i wait
    thanks

  3. #4 by wmc on December 25, 2012 - 9:27 pm

    Hi Tony,
    This is a great example. However, since am very new to android, l havent managed to get it running… l kindly request if you can assist me with the working code at mwemmicip(at)gmail.com?

  4. #5 by Mari on December 31, 2012 - 2:34 pm

    Hi Tony,
    great tutorial :*
    but may I get the full source code.
    My email is : s_mari20@yahoo.com

    Thank you soo much :)

  5. #6 by Flemming Astrup on February 3, 2013 - 2:51 pm

    Hallo Tony
    fine tutorial
    is it possible to get the full source code, or can i download it.
    thank you in advance Flemming

  6. #7 by Flemming Astrup on February 7, 2013 - 11:34 am

    Hallo Tony
    fine tutorial
    is it possible to get the full source code, or can i download it.
    thank you in advance Flemming
    fleastrup1@gmail.com

  7. #8 by CC on February 26, 2013 - 8:04 am

    Hi Tony,
    This is a great example but as I am new in android, I wasn’t able to get it running… Can you please assist me with the working code critika.capoor@gmail.com… Thank you so much.

  8. #9 by Pavan Deshpande on July 8, 2013 - 7:24 am

    Hi there that’s very nice example of implicit intent and it’s very nicely explained most of the time for beginner it’s began hard to understand for the first time
    you can also check the one at
    Implicit intent

  1. Bluetooth Data Transfer with Android « Tony's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: