DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Infrastructure as Code Is Not Enough
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach
  • Diving into JNI: My Messy Adventures With C++ in Android

Trending

  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Identity in Action
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • 5 AI Security Incidents That Broke Things in Production (and What They Have in Common)
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android SMS popup - Part Four: Implicit Intents

Android SMS popup - Part Four: Implicit Intents

By 
Tony Siciliani user avatar
Tony Siciliani
·
Oct. 05, 22 · Interview
Likes (1)
Comment
Save
Tweet
Share
14.1K Views

Join the DZone community and get the full member experience.

Join For Free

in  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:

  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  .

Intent (military) Android (robot) SMS

Opinions expressed by DZone contributors are their own.

Related

  • Infrastructure as Code Is Not Enough
  • Pragmatic Paths to On-Device AI on Android with ML Kit
  • Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach
  • Diving into JNI: My Messy Adventures With C++ in Android

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook