Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 11 / API 30 - Open YouTube link

I currently struggle to have YouTube links to be opened in the browser or the YouTube app. I am aware of the new Package visibility in Android 11 and implemented it this way.

MyClass.java$myMethod

String ytLink = "https://www.youtube.com/watch?v=Hp_Eg8NMfT0"
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(ytLink)); 
if (intent.resolveActivity(context.getPackageManager()) != null) {  
  context.startActivity(intent); 
}

AndroidManifest.xml

<manifest>
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="https" />
    </intent>
  </queries> 
</manifest>

However the intent.resolveActivity returns null... Every other link I've tried returns a ComponentName so the Intent can be started... Do you have any clue?

Thanks in advance ;)

like image 291
Doc_1faux Avatar asked Sep 05 '25 03:09

Doc_1faux


2 Answers

Yet another option would be to add the host to the queries list in the manifest:

<queries>    
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data  android:scheme="https" android:host="youtube.com" />
    </intent>
</queries>

It seems that the simple approach with just the scheme fails as the Youtube app not only filters for the scheme of the request but (for obvious reasons) also for the specific host.

BTW: When I tested, both "youtube.com" and "youtu.be" worked equally on both kinds of links. I still threw in both in my app to be safe.

like image 96
Sebastian Staacks Avatar answered Sep 07 '25 23:09

Sebastian Staacks


You also need to put action and data to intent-filter of Activity in Manifests

Such as

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />

    <data android:scheme="https" />
</intent-filter>

You can check the docs here

like image 34
Thành Thỏ Avatar answered Sep 07 '25 23:09

Thành Thỏ