Thursday, March 11, 2010

Youtube/Market Intent for Search in Android

Here is a trick that would help other applications that need to open search results for videos based on a particular keyword.

You need to create an implicit Intent and trigger it. Here is a snippet of code that works.

For Youtube
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Here, "Android" is the keyword. Now, the Youtube app will show you videos with the keyword "Android"

For Market
Similarly, by changing only the package name, you can bring up search results with the Android Market app as well.

Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.android.vending");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

For Contacts
The package name for the Contacts app is "com.google.contacts".

You just need the package name of the target app if you don't want the Resolver activity to show up. This might work for other google apps as well. I haven't checked though.