当通过 WebView (Android Studio) 导航到时,Youtube 在应用程序中启动
Youtube launches in an appliaction when navigated to via WebView (AndroidStudio)
我有一个简单的程序,它应该在应用程序启动时在 webView 中加载 youtube 网站,但该应用程序只是打开本机 Youtube 应用程序,但我需要它来在 webView 中打开 Youtube 网站。
我尝试查看 webView 设置中的 "attributes" 部分,但找不到任何可以强制在 webView 中打开此类网站的内容。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = findViewById(R.id.webV);
web.loadUrl("https://www.youtube.com/");
}
我希望 "www.youtube.com" 网站在我的 WebView 应用程序中打开,而不是在当前打开的本机 Youtube 应用程序中打开。
这是由于 youtube 视频 url 在浏览器中打开,浏览器将其重定向到本机应用程序
添加以下代码
web.setWebViewClient(new WebViewClient());
更新
When the user clicks a link from a web page in your WebView, the
default behavior is for Android to launch an application that handles
URLs. Usually, the default web browser opens and loads the destination
URL. However, you can override this behavior for your WebView, so
links open within your WebView. You can then allow the user to
navigate backward and forward through their web page history that’s
maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for
your WebView, using setWebViewClient().
我有一个简单的程序,它应该在应用程序启动时在 webView 中加载 youtube 网站,但该应用程序只是打开本机 Youtube 应用程序,但我需要它来在 webView 中打开 Youtube 网站。
我尝试查看 webView 设置中的 "attributes" 部分,但找不到任何可以强制在 webView 中打开此类网站的内容。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = findViewById(R.id.webV);
web.loadUrl("https://www.youtube.com/");
}
我希望 "www.youtube.com" 网站在我的 WebView 应用程序中打开,而不是在当前打开的本机 Youtube 应用程序中打开。
这是由于 youtube 视频 url 在浏览器中打开,浏览器将其重定向到本机应用程序
添加以下代码
web.setWebViewClient(new WebViewClient());
更新
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that’s maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient().