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

标签 java android android-activity webview youtube

各位程序员大家好。一些背景信息,我对 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/

相关文章:

java - 如何在 Davik VM 上将字符串整数有符号值转换为 2 字节数组?

java - android:如何更改按钮点击布局?

android - 获取 Intent 及其附加信息,但不是启动新 Activity 的 Activity 的 Intent

java - Android 应用程序自定义数字选择器

c# - 有没有从 Java netbeans 项目调用 .net 类的好方法?

java - 如何在android中格式化和打印文本文件?

java - 如何使用 AES 将字符串加密为仅包含特定字符的字符串?

android - 在 Android 中构建 PDFium

java - 如何完成主 Activity onBackPressed?

java - Android Studio音乐播放器无法从SD卡读取,只能从内存读取