List all the Activities

Use an Intent object to find every Activity with Intent.ACTION_MAIN and Intent.CATEGORY_LAUNCHER in every app on the phone. List their names in a TextView.

Source code in Fish.zip

  1. FishActivity.java
  2. main.xml: a ScrollView instead of a LinearLayout.
  3. AndroidManifest.xml: untouched.

Things to try

  1. Do you list a lot more activities (four or five hundred of them) if you don’t add the launcher category? For example, you should now see all the activities in the ApiDemos app.

  2. List the activities in alphabetical order; human beings prefer case insensitive. Is it safe to rearrange the items in the List we got from queryIntentActivities? Just in case it isn’t, create a copy of the List and then sort the copy. Instead of passing an anonymous OnClickListener object to the method setOnClickListener, we will pass an anonymous Comparator object to the method sort. loadLabel returns a CharSequence, which is not necessarily a String. That’s why we have to convert it toString before we can call the String method compareToIgnoreCase.
    		final List<ResolveInfo> copy = new ArrayList<ResolveInfo>(list);
    
    		Collections.sort(copy, new Comparator<ResolveInfo>() {
    			@Override
    			public int compare(ResolveInfo a, ResolveInfo b) {
    				final String aLabel = a.loadLabel(packageManager).toString();
    				final String bLabel = b.loadLabel(packageManager).toString();
    				return aLabel.compareToIgnoreCase(bLabel);
    			}
    		});
    
    Then display the copy instead of the original list.

  3. Instead of sorting the resolveInfos alphabetically, sort them in order of how closely they matched what we were looking for.