I’m developing a simple voice recognizer using the service offered by Android and reading the speech input article on Android Developers website. This article shows the following code:
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
...
The second argument of queryIntentActivities method is an int flag. If I understand it, this flag is used to add additional search options: in this case, what options are available for searching the specified Intent?
Why does the flag specified in the example is set to zero?
This should be filed as an SDK documentation issue in the Android Issue Mgmt System. I’ve had the same question. PackageManager is an abstract class and its subclasses ultimately end up calling the system service called “PackageManagerService” which implements the logic for #queryIntentActivities(Intent, int).
Until the documentation is improved the answer can be found in the source, for example the 1.5r4 source at http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/com/android/server/PackageManagerService.java#PackageManagerService.Settings.isEnabledLP%28android.content.pm.ComponentInfo%2Cint%29.
The “0” flag is very likely a remnant of some example/reference code that has been copied perpetually. The code at PackageManagerService isn’t easy to follow. It looks like com.android.server.IntentResolver#queryIntent(…) eventually gets called during the resolution process. This method’s callers are only concerned with the bit-value of the MATCH_DEFAULT_ONLY flag. Keep in mind that Intent flags can be OR’ed and AND’ed. What I’m trying to say is that #queryIntent(…) ultimately sees whether MATCH_DEFAULT_ONLY’s bit value is set or not.
In the current state of affairs it’s difficult to say with full confidence which flags actually work and how they work without significant experimentation and code analysis. I can say that PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, and PackageManager.COMPONENT_ENABLED_STATE_DISABLED are used.