Chapter 15 Implicit Intents
To respond to implicit intents, a DEFAULT category must be set explicitly in an intent filter. The action
element in the intent filter tells the OS that the activity is capable of performing the job, and the
DEFAULT category tells the OS that this activity should be considered for the job when the OS is asking
for volunteers. This DEFAULT category is implicitly added to every implicit intent. (In Chapter 24, you
will see that this is not the case when Android is not asking for a volunteer.)
Implicit intents can also include extras, just like explicit intents. Any extras on an implicit intent,
however, are not used by the OS to find an appropriate activity.
Note that the action and data parts of an intent can also be used in conjunction with an explicit intent.
That would be the equivalent of telling a particular activity to do something specific.
Sending a crime report
Let’s see how this works by creating an implicit intent to send a crime report in CriminalIntent. The
job you want done is sending plain text; the crime report is a string. So the implicit intent’s action will
be ACTION_SEND. It will not point to any data or have any categories, but it will specify a type of text/
plain.
In CrimeFragment.onCreateView(...), get a reference to the SEND CRIME REPORT button and
set a listener on it. Within the listener’s implementation, create an implicit intent and pass it into
startActivity(Intent).
Listing 15.9 Sending a crime report (CrimeFragment.java)
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckbox;
private Button mReportButton;
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
mReportButton = (Button) v.findViewById(R.id.crime_report);
mReportButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, getCrimeReport());
i.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.crime_report_subject));
startActivity(i);
}
});
return v;
}
Here you use the Intent constructor that accepts a string that is a constant defining the action. There
are other constructors that you can use depending on what kind of implicit intent you need to create.
You can find them all on the Intent reference page in the documentation. There is no constructor that
accepts a type, so you set it explicitly.