1

I want to register an intent-filter exclusively for the share in the Youtube app.

So far I'm able to receive the intent from Youtube successfully. The problem is my intent filer is not specific enough. My app is displayed as available for other share features in other apps (not only for Youtube).

This is what I'm using right now:

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

I have reviewed several questions (most are much like this one) the problem is there is an inaccuracy in those types of answers:

<data android:host="www.youtube.com" ... />

According to the data documentation the scheme most be provided in order for the host to be valid. So in those answers simply adding the host, doesn't make the intent-filter specific for Youtube, because there is no scheme, therefore, the host is ignored.

So I have been trying to figure this out by using the available methods of the intent when the Activity is started:

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();
    for (String key : bundle.keySet()) {
        Log.d("KEY", key);
    }

    //The above loop will log
    //... D/KEY: android.intent.extra.SUBJECT
    //... D/KEY: android.intent.extra.TEXT

    //This is are the same keys than above, but using the available constants
    String subject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
    String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);

    //The subject is the video title
    Log.d("SUBJECT", subject);

    //The text is the video url, example: https://youtu.be/p6qX_lg4wTc
    Log.d("TEXT", text);

    //Action is consistent with the intent-filter android.intent.action.SEND
    Log.d("ACTION", intent.getAction());

    //This is consistent with the intent-filter data mime type text/plain
    Log.d("TYPE", intent.getType());

    /*
      This is the problem.
      The scheme is null (that is why I'm using String value of).
    */
    Log.d("scheme", String.valueOf(intent.getScheme()));

So, when the available information in the intent is checked, everything seems to be in order, but not the scheme. So, based on what is gotten, I have done some blind attempts to figure it out:

<data android:scheme="http" android:mimeType="text/plain"/>

//I'm adding youtu.be here because is the url format in the text extra
<data android:scheme="http" android:host="youtu.be" android:mimeType="text/plain"/>

Adding http or https won't work, it makes the app is no longer available in the chooser. This means it will neither work the other attempt adding the host.

Doe's anybody knows how to create an intent-filter exclusively for Youtube share?

PS: I know I could validate the url to see if it match a Youtube url, but having my app in every chooser matching SEND doesn't seem user friendly

cutiko
  • 9,887
  • 3
  • 45
  • 59

2 Answers2

1

tldr: You can't make an intent-filter exclusively for Youtube app, the intent for sharing a video doesn't have anything to narrow it down.

So I got really obsessed about this, the only way to find out seems to check the Youtube app code. I found the apk here and then decompile with this. I think I found the code in this file (the text error seems to confirm my finding).

So, if my deductions are correct, then what is going on is Youtube app make an intent with no scheme or nothing else to make it specific. It only uses SEND.

cutiko
  • 9,887
  • 3
  • 45
  • 59
0

This not answer your specific question, but I think achieves the same you want.

You can use the youtube android player api library and the YouTubeIntents

Like this:

Intent youtubeIntent = 
  YouTubeIntents.createPlayVideoIntent(getApplicationContext(), VIDEO_ID);
startActivity(youtubeIntent);
Julio_oa
  • 570
  • 6
  • 15
  • I want my app to receive an explicit intent from Youtube, I don't want to send and intent from my app to Youtube. I can receive the intent from Youtube, my app is registered as available using and intent-filter. When the user is on Youtube press the share button my app is available in the chooser. What I need to do now is to make it available for Youtube exclusively, because is also available in the coposer for other apps that can trigger a SEND implicit intent. For making it available for only Youtube and not others I have to make my intent-filter more specific. – cutiko Nov 07 '17 at 12:07