You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中点击YouTube视频后如何获取URL并显示到TextView?

解决方案:获取YouTube视频URL并返回Android应用

一、使用WebView实现

通过拦截WebView中的URL跳转,识别YouTube视频链接后关闭WebView并将URL显示到TextView。

1. 布局文件(activity_main.xml)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/openYoutubeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开YouTube"/>

    <TextView
        android:id="@+id/videoUrlTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"/>

    <WebView
        android:id="@+id/youtubeWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>

2. 主Activity代码(MainActivity.java)

import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private WebView youtubeWebView;
    private TextView videoUrlTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        youtubeWebView = findViewById(R.id.youtubeWebView);
        videoUrlTv = findViewById(R.id.videoUrlTv);
        Button openYoutubeBtn = findViewById(R.id.openYoutubeBtn);

        // 启用JavaScript并配置WebViewClient拦截URL
        youtubeWebView.getSettings().setJavaScriptEnabled(true);
        youtubeWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                // 精准匹配YouTube视频watch页面链接
                if (url.matches("https?://(www\\.|m\\.)?youtube\\.com/watch\\?v=[^&]+")) {
                    // 隐藏WebView
                    youtubeWebView.setVisibility(View.GONE);
                    // 将URL设置到TextView
                    videoUrlTv.setText("选中的视频URL:" + url);
                    return true; // 拦截该URL,不继续加载
                }
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

        openYoutubeBtn.setOnClickListener(v -> {
            youtubeWebView.setVisibility(View.VISIBLE);
            // 加载移动端YouTube页面,适配WebView
            youtubeWebView.loadUrl("https://m.youtube.com");
        });
    }
}

3. 权限配置

在AndroidManifest.xml中添加网络权限:

<uses-permission android:name="android.permission.INTERNET"/>

二、使用Chrome Custom Tab实现

通过自定义Intent Filter接收YouTube视频链接,关闭Custom Tab后将URL传递回主Activity。

1. 依赖配置

在build.gradle(Module级别)中添加Chrome Custom Tab依赖:

dependencies {
    implementation "androidx.browser:browser:1.6.0"
}

2. 注册URL接收Activity

在AndroidManifest.xml中添加VideoUrlReceiverActivity的配置:

<activity android:name=".VideoUrlReceiverActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <!-- 匹配YouTube视频链接 -->
        <data
            android:host="youtube.com"
            android:pathPattern="/watch*"
            android:scheme="https"/>
        <data
            android:host="m.youtube.com"
            android:pathPattern="/watch*"
            android:scheme="https"/>
    </intent-filter>
</activity>

3. 主Activity代码(MainActivity.java)

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;

public class MainActivity extends AppCompatActivity {
    private TextView videoUrlTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoUrlTv = findViewById(R.id.videoUrlTv);
        Button openYoutubeBtn = findViewById(R.id.openYoutubeBtn);

        openYoutubeBtn.setOnClickListener(v -> {
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            // 打开移动端YouTube页面
            customTabsIntent.launchUrl(this, Uri.parse("https://m.youtube.com"));
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 接收传递回来的视频URL并显示
        String videoUrl = intent.getStringExtra("VIDEO_URL");
        if (videoUrl != null) {
            videoUrlTv.setText("选中的视频URL:" + videoUrl);
        }
    }
}

4. URL接收Activity代码(VideoUrlReceiverActivity.java)

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class VideoUrlReceiverActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri uri = getIntent().getData();
        if (uri != null) {
            String videoUrl = uri.toString();
            // 关闭当前页面(关联的Chrome Custom Tab会自动关闭)
            finish();
            // 将URL传递回主Activity
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("VIDEO_URL", videoUrl);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        } else {
            finish();
        }
    }
}

内容的提问来源于stack exchange,提问作者Akshay Vadchhak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 19:17:06