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.

7 comments:

  1. Thank you for sharing this trick. It is so useful.. but it do not work on CupCake: itent.setPackage(package) is undefined in API Level 3. So I found an way to do that:

    Intent toYoutubeIntent = new Intent();

    toYoutubeIntent .setAction(Intent.ACTION_SEARCH);
    toYoutubeIntent .setClassName("com.google.android.youtube", "com.google.android.youtube.QueryActivity");
    toYoutubeIntent .putExtra("query", "Android");
    toYoutubeIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    startActivity(toYoutubeIntent );

    May be will be useful for someone as your code was for me!

    Thank your!

    ReplyDelete
  2. Thanks for the trick.
    I want to achive something similar, using cupcake: I want to open a given channel. Any idea how to achieve this?

    ReplyDelete
  3. is there anyway to trigger onActivityResult. I am having some hard time doing that.

    ReplyDelete
  4. Hmmm, I don't think the Youtube app sends back result. So, it will not help.

    ReplyDelete
  5. The above techniques didn't work. The one below does work -at the time of this writing-:

    Intent intent = new Intent("android.intent.action.MEDIA_SEARCH");
    intent.setPackage("com.google.android.youtube");
    intent.putExtra("query", query); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    ReplyDelete
  6. thank you, exactly what i looking for!

    ReplyDelete