IntentNotFoundException for TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA - android

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.

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.

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" />

Related

Get package name from Intent

I am trying to get the package name of the default android clock (stock clock).
This is the intent I use to open the stock clock.
Intent i = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
I have tried
i.getPackage();
But this returns null. Is there a way to get the package name of the default clock in android, in any phone?
But this returns null
That is because you have not set the package name in the Intent.
Is there a way to get the package name of the default clock in android, in any phone?
Not really, as there is no concept of a "default clock" in Android.
However, you can use PackageManager and queryIntentActivities() to see what activities will respond to the Intent that you have constructed.
Hope this will help you:
final PackageManager packageManager = this.getPackageManager();
Intent intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
List<ResolveInfo> packages = packageManager.queryIntentActivities(intent,0);
for(ResolveInfo res : packages){
String package_name = res.activityInfo.packageName;
Log.w("Package Name: ",package_name);
}

Why doesn't my App Chooser dialogue show?

I am using this tutorial to learn about the basics of Intents in Android. The code sample I am using is adopted from the section named "Force an App Chooser" on the page linked.
The following is the method which should be invoked on the click of the button.
public void startSecondActivity(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
String fileChooserLabel = getResources().getString(R.string.fileChooserLabel);
Intent fileChooserIntent = Intent.createChooser(intent, fileChooserLabel);
if (intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
} else {
textView = (TextView) findViewById(R.id.text_view);
textView.setText("False");
}
}
But it just enters the else block of the if-else conditional. I tried this app on both a real device and the emulator. So can anybody point out what might be wrong here, and what I can do about it.
Note: I did not add anything to the Manifest file, since I am using Eclipse IDE and I suppose whatever is required at this point is automatically added to the manifest file.
It is returning null because there are no activities on the device that support your Intent. In this case, your ACTION_SEND Intent is not properly set up.
Note that the code sample you used as a reference is not a tutorial. It is not designed to be a complete code sample. In fact, what they list there will not even compile, as their ... is meant to be replaced by your own code to complete setting up the Intent.
You will need to fully configure your ACTION_SEND Intent, most notably setting the MIME type, as is covered elsewhere in the documentation. Replacing:
Intent intent = new Intent(Intent.ACTION_SEND);
with something like:
Intent intent = new Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, "IM IN UR STAK OVERFLO ANZWR");
should suffice.

Tweeting by using intent from android app

I'm using the following code to tweet something from another app:
try{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "It's a Tweet!" + "#MyApp");
intent.setType("text/plain");
final PackageManager pm = getPackageManager();
final List<?> activityList = pm.queryIntentActivities(intent, 0);
int len = activityList.size();
for (int i = 0; i < len; i++) {
final ResolveInfo app = (ResolveInfo) activityList.get(i);
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
final ActivityInfo activity=app.activityInfo;
final ComponentName x=new ComponentName(activity.applicationInfo.packageName, activity.name);
intent=new Intent(Intent.ACTION_SEND);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setComponent(x);
intent.putExtra(Intent.EXTRA_TEXT, "blah blah" );
startActivity(intent);
break;
}
}
} catch(final ActivityNotFoundException e) {
Log.i("Twitter intent", "no twitter native", e );
}
The above code works perfectly if the twitter app is installed on the phone but is not open. But if I was using twitter before using the above activity, then I just simply get transferred to the same point where I last left twitter and the text that I wanted to tweet is not posted. What should I do to correct this?
Also, if twitter is not installed on my phone, then nothing happens. On pressing the button that starts the intent above nothing happens but shouldn't I actually be getting the message - no twitter native. What should be done to get the message??
I think you should use setClassName instead of setComponent.
intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
But as a helpful side note, There are several options for you to integrate with twitter:
1)Use their API (maybe use a library such as twitter4j), or
2)Find a way to integrate with the Twitter app, like you are doing right now. This is not a good idea because many people use other apps such as Plume, TweetDeck and so on and your app wont work if they dont have twitter installed.
3)you can just open up http://twitter.com/?status= using a simple intent.
This can be done like:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));
startActivity(i);
You can also combine 2 and 3. You can try a few known twitter apps and if they are absent, call the browser.
Also note that it might be possible for method 3 to actually launch an app instead of the browser (or at least give the user a choice between the two), if the app handles the correct intents.
In my opinion, Method 1 is best since it only depends on your app ( well, except for the library) and you don't have to integrate with third party apps that the user may or may not have.
Simple remove Intent.FLAG_ACTIVITY_NEW_TASK for Intent.FLAG_ACTIVITY_CLEAR_TASK, so the task where the twitter is running gets re-started.
Your flags should look like this now:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Hope this helps, and remember to accept the answer if correct :)

How to call activity of one project from activity of another project in android?Also vice versa?

I am doing an integration project,which involves integrating two projects into one.How I want to do this is,I have a common project,the activity of this common project should be able to call activities of the other two projects,as per different events like a particular button press,etc.How can I do this?Is it possible through intents?
Also,the activities of the other two projects should be able to call each other.How to do this?
this Android Developer blog post explains how to make custom application intents available to other applications for this sort of integration:
http://android-developers.blogspot.com/2009/11/integrating-application-with-intents.html
You will have to use intent filters, Sample code below
PackageManager packageManager = getPackageManager();
Intent baseIntent = new Intent(ACTION_PICK_PLUGIN);
baseIntent.addCategory("matching.catagory");
List<ResolveInfo> activities = packageManager.queryIntentActivities(baseIntent, PackageManager.GET_RESOLVED_FILTER);
Then fire intent using the following,
Intent baseIntent = new Intent(activities.get(indexOfChild).filter.getAction(0));
baseIntent.addCategory(activities.get(indexOfChild).filter.getCategory(0));
baseIntent.setComponent(newComponentName(activities.get(indexOfChild).activityInfo.packageName,activities.get(indexOfChild).activityInfo.name));
startActivity(baseIntent);
I hope it helps..
If you want to call the MainActivity of a project from the existing project and vice versa you can use PackageManager class
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("Target package");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.ACTION_VIEW );
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}

How can I check if the Android Market is installed on my user's device?

How could I write a code which can tell me that android market is installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager() and getApplicationInfo() (if the package is not found, a PacketManager.NameNotFoundException will be thrown - see here). Android Market's package name is com.android.vending.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}

Categories

Resources