Android SMS popup - Part Four: Implicit Intents
Join the DZone community and get the full member experience.
Join For Freein part one , we captured sms messages using a broadcastreceiver. in , 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 :
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:
- close the sms popup window once the message is read
- 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 .
Opinions expressed by DZone contributors are their own.
Comments