Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 15  Implicit Intents


In CrimeFragment.java, add a method that creates four strings and then pieces them together and
returns a complete report.


Listing 15.8  Adding getCrimeReport() method (CrimeFragment.java)


private void updateDate() {
mDateButton.setText(mCrime.getDate().toString());
}


private String getCrimeReport() {
String solvedString = null;
if (mCrime.isSolved()) {
solvedString = getString(R.string.crime_report_solved);
} else {
solvedString = getString(R.string.crime_report_unsolved);
}


String dateFormat = "EEE, MMM dd";
String dateString = DateFormat.format(dateFormat,
mCrime.getDate()).toString();


String suspect = mCrime.getSuspect();
if (suspect == null) {
suspect = getString(R.string.crime_report_no_suspect);
} else {
suspect = getString(R.string.crime_report_suspect, suspect);
}


String report = getString(R.string.crime_report,
mCrime.getTitle(), dateString, solvedString, suspect);


return report;
}
}


(Note that there are two DateFormat classes: android.text.format.DateFormat and
java.text.DateFormat. Use android.text.format.DateFormat.)


Now the preliminaries are complete, and you can turn to implicit intents.


Using Implicit Intents


An Intent is an object that describes to the OS something that you want it to do. With the explicit
intents that you have created thus far, you explicitly name the activity that you want the OS to start,
like:


Intent intent = new Intent(getActivity(), CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
startActivity(intent);


With an implicit intent, you describe to the OS the job that you want done. The OS then starts the
activity that has advertised itself as capable of doing that job. If the OS finds more than one capable
activity, then the user is offered a choice.

Free download pdf