基于WebView的应用,YouTube视频没有全屏按钮

时间:2016-05-11 16:38:35

标签: android wordpress android-studio webview youtube

我有一个基于WebView的应用程序,显示一个WordPress网站。我在我的网站上有几个YouTube视频,问题是YouTube视频没有全屏按钮。我寻找答案,并没有在网上找到任何东西。有人能帮我吗?如何让YouTube视频可以从我的WebView全屏播放? 这是我的MainActivity代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.delitestudio.pushnotifications.PushNotifications;
import com.loopj.android.http.*;

import cz.msebera.android.httpclient.*;
import cz.msebera.android.httpclient.impl.entity.StrictContentLengthStrategy;

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final WebChromeClient webChromeClient = new WebChromeClient();
    private static final WebViewClient webViewClient = new WebViewClient();

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

        String url;
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.containsKey("fromPush")) {
            url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
            WebView myWebView = (WebView) findViewById(R.id.webView);
            myWebView.loadUrl(url);
        } else {
            url = "http://paotiptipon.co.il/";
        }

        WebView myWebView = (WebView) this.findViewById(R.id.webView);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(webChromeClient);
        myWebView.setWebViewClient(webViewClient);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setAllowContentAccess(true);
        myWebView.getSettings().setDatabaseEnabled(true);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.loadUrl(url);

        myWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    WebView webView = (WebView) v;

                    switch (keyCode) {
                        case KeyEvent.KEYCODE_BACK:
                            if (webView.canGoBack())
                                webView.goBack();
                            break;
                    }
                }
                return false;
            }
        });
        notificationsButtonClicked();
        newNotificationButtonClicked();
        calendarButtonClicked();

        final PushNotifications pn = new PushNotifications(this);
        if (pn.getToken() == null || pn.isExpired()) {
            pn.refreshToken("http://www.paotiptipon.co.il/pnfw/register/",
                    "35299935473");
        }

//        Bundle extras = getIntent().getExtras();
//        if (extras != null) {
//            int id = extras.getInt(PushNotifications.ID);
//            String title = extras.getString(PushNotifications.TITLE);
//            System.out.println(id + title);
//        }
    }

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            mIntentReceiver.abortBroadcast();

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
            alertDialog.setTitle("התראה חדשה!");
            alertDialog.setMessage("יש התראה חדשה במרכז ההתראות! לחצו על אישור כדי לראות אותה!");
            // Setting OK Button
                alertDialog.setPositiveButton("אישור",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
                        WebView myWebView = (WebView) findViewById(R.id.webView);
                        myWebView.loadUrl(url);
                    }
                });
            // Setting CANCEL Button
            alertDialog.setNegativeButton("ביטול",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            alertDialog.show();
        }
    };

    private void notificationsButtonClicked() {
        Button notifications = (Button) findViewById(R.id.notifications);
        notifications.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
                WebView myWebView = (WebView) findViewById(R.id.webView);
                myWebView.loadUrl(url);
            }
        });
    }

    private void newNotificationButtonClicked() {
        Button newNotification = (Button) findViewById(R.id.newNotification);
        newNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.paotiptipon.co.il/wp-admin/post-new.php";
                WebView myWebView = (WebView) findViewById(R.id.webView);
                myWebView.loadUrl(url);
            }
        });
    }

    private void calendarButtonClicked() {
        Button calendar = (Button) findViewById(R.id.calendar);
        calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.paotiptipon.co.il/events/";
                WebView myWebView = (WebView) findViewById(R.id.webView);
                myWebView.loadUrl(url);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mIntentReceiver, PushNotifications.getIntentFilter(this));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mIntentReceiver);
    }


}

1 个答案:

答案 0 :(得分:0)

首先感谢那些试图提供帮助的人。 我找到了我的问题的答案,我想在这里分享,所以如果有人需要这个,它就会在这里。 我用这个链接: Playing HTML5 video on fullscreen in android webview 我刚刚从那里下载了示例项目,并在我的项目中实现了代码。写这些课程的人很棒,写了一篇非常好的教程,解决了我的问题。 :)