Chapter 24 More About Intents and Tasks
Resolving an Implicit Intent
NerdLauncher will show the user a list of launchable apps on the device. (A launchable app is an app
the user can open by clicking an icon on the Home or launcher screen.) To do so, it will query the
system (using the PackageManager) for launchable main activities. Launchable main activities are
simply activities with intent filters that include a MAIN action and a LAUNCHER category. You have seen
this intent filter in the AndroidManifest.xml file in your projects:
In NerdLauncherFragment.java, add a method named setupAdapter() and call that method from
onCreateView(...). (Ultimately this method will create a RecyclerView.Adapter instance and set it
on your RecyclerView object. For now, it will just generate a list of application data.) Also, create
an implicit intent and get a list of activities that match the intent from the PackageManager. Log the
number of activities that the PackageManager returns.
Listing 24.3 Querying the PackageManager (NerdLauncherFragment.java)
public class NerdLauncherFragment extends Fragment {
private static final String TAG = "NerdLauncherFragment";
private RecyclerView mRecyclerView;
public static NerdLauncherFragment newInstance() {
return new NerdLauncherFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
setupAdapter();
return v;
}
private void setupAdapter() {
Intent startupIntent = new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = getActivity().getPackageManager();
List
Log.i(TAG, "Found " + activities.size() + " activities.");
}
}
Run NerdLauncher and check Logcat to see how many apps the PackageManager returned. (We got 30
the first time we tried it.)