In Android, you can start an activity in another application on the device using an implicit intent. In an explicit intent, you specify the class of the activity to start, and the OS will start it. In an implicit intent, you describe the job that you need done, and the OS will start an activity in an appropriate application for you.
In CriminalIntent, you will use implicit intents to enable picking a suspect for a Crime from the user’s list of contacts and sending a text-based report of a crime. The user will choose a suspect from whatever contacts app is installed on the device and will be offered a choice of apps to send the crime report (Figure 15.1).
Using implicit intents to harness other applications is far easier than writing your own implementations for common tasks. Users also appreciate being able to use apps they already know and like in conjunction with your app.
Before you can create these implicit intents, there is some setup to do in CriminalIntent:
add CHOOSE SUSPECT and SEND CRIME REPORT buttons to CrimeFragment’s layouts
add an mSuspect field to the Crime class that will hold the name of a suspect
create a crime report using a set of format resource strings
You are going to start by updating CrimeFragment’s layout to include new buttons for accusation and tattling: namely, a suspect button and a report button. First, add the strings that these buttons will display.
Listing 15.1 Adding button strings (strings.xml
)
<string name="subtitle_format">%1$d crimes</string> <string name="crime_suspect_text">Choose Suspect</string> <string name="crime_report_text">Send Crime Report</string> </resources>
In layout/fragment_crime.xml, add two button widgets, as shown in Figure 15.2. Notice that in this diagram we are not showing all of the LinearLayout’s children so that you can focus on the new and interesting parts of the diagram on the right.
At this point, you can preview the updated layout or run CriminalIntent to confirm that your new buttons are in place.
3.15.174.76