Over a million developers have joined DZone.

Android SMS popup - Part Four: Implicit Intents

DZone's Guide to

Android SMS popup - Part Four: Implicit Intents

· Mobile Zone ·
Free Resource
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.putExtra("msg", pop_msg);

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

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);
    	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);

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.

Source:  Tony's Blog.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}