2
votes

enter image description hereI am making an android music player application in which i want to add an search functionality that's why i need to implement this like:

I want to search the song name and create the chooser for youtube and all installed browser with the song title and not for the url

I think i am unable to describe my question so please see the below may be it will help you to understand:-

Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://stackoverflow.com/"));
//here i want to set like that intent.setData("my song title")
            startActivity(Intent.createChooser(intent,"search")); 

but if i am doing this it show me -NO APPS CAN PERFORM THIS ACTION

so idon't know how to put the title as url in intent

please remember this user also able to search this on youtube

1

1 Answers

2
votes

You can start two separate searches with your query as follows:

Youtube Search

Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra(SearchManager.QUERY, "Sunday Bloody Sunday");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Browser Search

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "Sunday Bloody Sunday");
startActivity(intent);

Note that if the song is well known, one of the first results of the browser search will be the Youtube video.

Edit

Here is auto to create a search query for both Youtube and browsers:

private void startSearchIntent(String query) {
    List<Intent> browserIntents = new ArrayList<>();
    // Find browser intents
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_WEB_SEARCH), 0);

    // Create a search intent for each browser that was found
    for (ResolveInfo info : resInfo) {
        Intent browserIntent = new Intent(Intent.ACTION_WEB_SEARCH);
        browserIntent.putExtra(SearchManager.QUERY, query);
        browserIntent.setPackage(info.activityInfo.packageName.toLowerCase());
        browserIntents.add(browserIntent);
    }

    // Create Youtube Intent
    Intent youtubeIntent = new Intent(Intent.ACTION_SEARCH);
    youtubeIntent.setPackage("com.google.android.youtube");
    youtubeIntent.putExtra(SearchManager.QUERY, query);
    youtubeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Intent chooserIntent = Intent.createChooser(youtubeIntent, query);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, browserIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);

}

Then you just have to call the method:

startSearchIntent("Sunday Bloody Sunday");

Hope it helps.