Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何在webview活动中显示Youtube频道?_Java_Android_Android Activity_Webview_Youtube - Fatal编程技术网

Java Android:如何在webview活动中显示Youtube频道?

Java Android:如何在webview活动中显示Youtube频道?,java,android,android-activity,webview,youtube,Java,Android,Android Activity,Webview,Youtube,各位程序员好。一些背景信息,我对android应用程序开发相当陌生,决定做一个个人项目来练习。在这个项目中,我遇到的大多数问题都可以通过各种谷歌搜索来解决。然而,这一问题在相当长一段时间内一直很明显 我想在网络视图中显示YouTube频道。我研究了其他相关论坛,但没有找到有效的解决方案。如果你想要一个这样的工作示例,请查看“Markiplier非官方应用程序”,它利用网络视图来显示和播放他的YouTube视频。下面是我到目前为止为这个活动编写的代码 Webview布局: <?xml ver

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

我想在网络视图中显示YouTube频道。我研究了其他相关论坛,但没有找到有效的解决方案。如果你想要一个这样的工作示例,请查看“Markiplier非官方应用程序”,它利用网络视图来显示和播放他的YouTube视频。下面是我到目前为止为这个活动编写的代码

Webview布局:

<?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>
webSettings.setDomStorageEnabled(true);
WebView活动:

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();
    }
 }
}
对此问题的任何指导都将不胜感激


谢谢。

将您的URL从www.youtube.com更改为

将您的URL从www.youtube.com更改为

更新您的代码,如下所示:

@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)) {
        view.loadUrl(url);
        return true; // Ensures URL opens in app
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

更新您的代码,如下所示:

@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)) {
        view.loadUrl(url);
        return true; // Ensures URL opens in app
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

好吧,在阅读了所有人的答案/建议并尝试之后,不幸的是,它们没有起作用。在又一次的研究热潮中,我终于找到了解决办法。请记住,从性能角度来看,这并不是最好的方法,但对于像我这样的新手来说,它是有效的

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

<?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>
webSettings.setDomStorageEnabled(true);
我希望这对某人有帮助。下面是我找到这个答案的问题的链接/URL(奇怪的是它一直躲过我的眼睛):


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

好吧,在阅读了所有人的答案/建议并尝试之后,不幸的是,它们没有起作用。在又一次的研究热潮中,我终于找到了解决办法。请记住,从性能角度来看,这并不是最好的方法,但对于像我这样的新手来说,它是有效的

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

<?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>
webSettings.setDomStorageEnabled(true);
我希望这对某人有帮助。下面是我找到这个答案的问题的链接/URL(奇怪的是它一直躲过我的眼睛):


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

嘿,UserKh,我在这里发布之前很久就试过了。我尝试了不同的URL变体,结果都失败了。没关系,我找到了答案。如果你感兴趣,请检查我对这个问题的回答。谢谢。嘿,我在这里发布之前很久就试过了。我尝试了不同的URL变体,结果都失败了。没关系,我找到了答案。如果你感兴趣,请检查我对这个问题的回答。谢谢。不幸的是,上面的代码没有任何区别,因为我仍然得到一个空白页。为了确保不仅仅是YouTube,我也尝试了google.com,但我仍然得到了同样的结果。奇怪的是,这对Facebook和Twitter都有效。还有其他想法吗?没关系,想出来了。如果你感兴趣,请检查我对这个问题的回答。谢谢。不幸的是,上面的代码没有任何区别,因为我仍然得到一个空白页。为了确保不仅仅是YouTube,我也尝试了google.com,但我仍然得到了同样的结果。奇怪的是,这对Facebook和Twitter都有效。还有其他想法吗?没关系,想出来了。如果你感兴趣,请检查我对这个问题的回答。谢谢