gpt4 book ai didi

android - Webview 链接到 m.youtube.com 而不是 youtube 应用

转载 作者:行者123 更新时间:2023-12-03 06:04:09 25 4
gpt4 key购买 nike

我有我的应用程序,它打开一个网页,上面有 youtube 视频的链接。

我有两个问题:

  • 链接可以正常打开 m.youtube.com,但无法播放视频。只是以橙色突出显示然后没有任何 react 。
  • 没有选项可以在 youtube 应用程序中加载。

  • 有任何想法吗?我在我的 Galaxy S2 而不是模拟器上进行测试,因为我知道 Emu 不会有 Flash 或 youtube 应用程序。

    非常感谢您的帮助,您将成为救生员!

    这是我的代码:
     package com.mytest;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class mytestActivity extends Activity

    {

    final Activity activity = this;

    private WebView webview;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the BACK key and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    webview.goBack();
    return true;
    }
    // If it wasn't the BACK key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);

    // Don't create another webview reference here,
    // just use the one you declared at class level.
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setPluginsEnabled(true);

    webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)
    {
    activity.setTitle("Loading...");
    activity.setProgress(progress * 100);

    if(progress == 100)
    activity.setTitle(R.string.app_name);
    }
    });

    webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    {
    // Handle the error
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
    view.loadUrl(url);
    return true;
    }


    });


    webview.loadUrl("http://mytesturl");

    }

    最佳答案

    我在我的 WebViewClient 中有这个,它会打开原生应用程序的 youtube 视频:

     @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.contains("youtube.com")){

    int indexStart = url.indexOf("?v=")+3;
    int indexEnd = url.indexOf("&", indexStart);
    if(indexEnd<indexStart)
    return false;
    String videoId = url.substring(indexStart,indexEnd);
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://"+videoId)));

    return true;
    }
    else
    {
    return false;
    }
    }

    关于android - Webview 链接到 m.youtube.com 而不是 youtube 应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455883/

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