4.5.9. shareSearch Method

Method shareSearch (Fig. 4.26) is called by the event handler in Fig. 4.25 when the user selects to share a search. Lines 217–218 create a String representing the search to share. Lines 221–227 create and configure an Intent that allows the user to send the search URL using an Activity that can handle the Intent.ACTION_SEND (line 222).


213      // allows user to choose an app for sharing a saved search's URL
214      private void shareSearch(String tag)
215      {
216         // create the URL representing the search
217         String urlString = getString(R.string.searchURL) +
218            Uri.encode(savedSearches.getString(tag, ""), "UTF-8");
219
220         // create Intent to share urlString
221         Intent shareIntent = new Intent();
222         shareIntent.setAction(Intent.ACTION_SEND );     
223         shareIntent.putExtra(Intent.EXTRA_SUBJECT,     
224            getString(R.string.shareSubject ));          
225         shareIntent.putExtra(Intent.EXTRA_TEXT,         
226            getString(R.string.shareMessage, urlString));
227         shareIntent.setType("text/plain" );             
228
229         // display apps that can share text            
230         startActivity(Intent.createChooser(shareIntent,
231            getString(R.string.shareSearch )));         
232      }
233


Fig. 4.26 | shareSearch method of class MainActivity.

Adding Extras to an Intent

An Intent includes a Bundle of extras—additional information that’s passed to the Activity that handles the Intent. For example, an e-mail Activity can receive extras representing the e-mail’s subject, CC and BCC addresses, and the body text. Lines 223–226 use Intent method putExtra to add an extra as a key–value pair to the Intent’s Bundle. The method’s first argument is a String key representing the purpose of the extra and the second argument is the corresponding extra data. Extras may be primitive type values, primitive type arrays, entire Bundle objects and more—see class Intent’s documentation for a complete list of the putExtra overloads.

The extra at lines 223–224 specifies an e-mail’s subject with the String resource R.string.shareSubject ("Twitter search that might interest you"). For an Activity that does not use a subject (such as sharing on a social network), this extra is ignored. The extra at lines 225–226 represents the text to share—a formatted String in which the urlString is substituted into the String resource R.string.shareMessage ("Check out the results of this Twitter search: %s"). Line 227 sets the Intent’s MIME type to text/ plain—such data can be handled by any Activity capable of sending plain text messages.

Displaying an Intent Chooser

To display the intent chooser shown in Fig. 4.8(a), we pass the Intent and a String title to Intent’s static createChooser method (line 230). The resource R.string.shareSearch ("Share Search to:") is used as the intent chooser’s title. It’s important to set this title to remind the user to select an appropriate Activity. You cannot control the apps installed on a user’s phone or the Intent filters that can launch those apps, so it’s possible that incompatible activities could appear in the chooser. Method createChooser returns an Intent that we pass to startActivity to display the intent chooser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.66