gpt4 book ai didi

java - Android:如何在 webview Activity 中显示 Youtube channel ?

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:02 25 4
gpt4 key购买 nike

各位程序员大家好。一些背景信息,我对 Android 应用程序开发相当陌生,并决定从事一个个人项目来练习我的手。在做这个项目的过程中,我遇到的大多数问题,我都可以通过各种谷歌搜索来解决。然而,这个问题已经存在很长一段时间了。

我想在 WebView 中显示 YouTube channel 。我研究了其他相关论坛,但没有找到有效的解决方案。如果您想要一个有效的示例,请查看“Markiplier Unofficial App”,它利用 WebView 来显示和播放他的 YouTube 视频。以下是我迄今为止为此 Activity 编写的代码。

Web View 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/userOptionsLinearLayout"
android:background="#000000">

<WebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|bottom" />
</LinearLayout>

自定义 WebViewClient:

import android.webkit.WebViewClient;
import android.webkit.WebView;

import android.net.Uri;
import android.content.Intent;

public class AJSYTWebViewClient extends WebViewClient{
String site_url_one = "www.youtube.com";
String site_url_two = "m.youtube.com";

// Used to judge which URL to keep open in app and which ones to divert to web browser or another app.
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().startsWith(site_url_one) || Uri.parse(url).getHost().startsWith(site_url_two)) {
return false; // Ensures URL opens in app
}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

WebView Activity :

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class ajs_youtube_page extends AppCompatActivity {

// Declaring Programming Webview variable.
private WebView mwebView;

// Primary URL.
String url = "www.youtube.com/user/AngryJoeShow";

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

// Finding webview on layout screen.
mwebView = (WebView) findViewById(R.id.activity_main_webview);
//mwebView.setWebChromeClient(new WebChromeClient());

// Getting and adjusting WebView settings for javascript functionality.
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

// Force links and redirects to open in the WebView instead of in a browser
mwebView.setWebViewClient(new AJSYTWebViewClient());

mwebView.loadUrl(url);
}

// When the back button is pressed, it goes back to the previous screen.
@Override
public void onBackPressed() {
if(mwebView.canGoBack()) {
mwebView.goBack();
}
else {
super.onBackPressed();
}
}
}

关于此问题的任何指导将不胜感激。

谢谢。

最佳答案

好吧,在阅读了每个人的答案/建议并尝试之后,不幸的是它们不起作用。经过又一次疯狂的研究,我终于找到了解决方案。请记住,这不是性能方面最好的方法,但对于像我这样的新学习者来说它是有效的。

您需要将以下代码添加到调用 webview 布局的 Activity 中:

webSettings.setDomStorageEnabled(true);

我希望这对某人有帮助。下面是我找到这个答案的问题的链接/URL(很惊讶它一直回避我的眼睛):Android webview won't load my URL but will load others

感谢所有回复的人。感谢您的意见。

关于java - Android:如何在 webview Activity 中显示 Youtube channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com