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 36 posts at DZone. You can read more from them at their website. View Full User Profile

Android SMS popup - Part Four: Implicit Intents

04.09.2012
| 4803 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 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.

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