views:

103

answers:

2

I am trying to implement text to speech by following this article on the Android Developers Blog. It suggests the following code for installing text to speech data if it is not supported.

Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);

This throws an Exception:

ActivityNotFoundException: No activity found to handle Intent

However, I am using the code here to determine the the intent is actually supported. Here is the list representation:

[ResolveInfo{43cc5280 com.svox.pico.DownloadVoiceData p=0 o=0 m=0x108000}]

Why doesn't this work?

Update

I don't know why, but it seems to work now.

A: 

To check whether the intent is actually supported or not, use the following code :

PackageManager pm = getPackageManager();
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
ResolveInfo resolveInfo = pm.resolveActivity( installIntent, PackageManager.MATCH_DEFAULT_ONLY );

if( resolveInfo == null ) {
   // Not able to find the activity which should be started for this intent
} else {
   startActivity( installIntent );
}

If it is not able to find the activity using resolveActivity() then it means that the activity requires some other parameters which are not provided. In that case, you should get the class name using the queryIntentActivities() and set the intent component/class name.

Karan
Your code for checking if an intent is supported is almost the same as that I am using from the [Android Dev Blog](http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html) and linked to from my post, except with queryIntentActivities swapped with resolveActivity.
Casebash
When you don't specify the component name for intent, then it internally uses the resolveActivity() method to resolve activity using the given parameters. And if the CATEGORY_DEFAULT is not specified in the intent filter of the class that matches then it will return you null. Whereas queryIntentActivities does not check for CATEGORY_DEFAULT parameter.
Karan
Btw can you please paste the logcat output here ? The IntentResolver details are displayed in the logcat.
Karan
A: 

What version of Android SDK are you aiming at with your code? Remember that TTS is only available from 1.6 (SDK Level 4) onwards. That code works just ok with 2.0 (SDK Level 5).

<uses-sdk android:minSdkVersion="5" />
Fernando Miguélez
My min SDK is 3, but I am targeting 7. I do have a wrapper class though to ensure it only gets called on the appropriate version. I will check this again
Casebash
That might be the problem. Try increasing to 5.
Fernando Miguélez