4

这是我在论坛上的第一个问题。

我正在构建一个应用程序,它实现了一个从 SD 卡加载 html5 页面的简单 web 视图。

该应用程序是一个由 5 个部分组成的演示文稿,每个部分都包含一些覆盖在大约 2 秒长的循环播放的高清视频顶部的文本,基本上就像一个带有视频背景的页面。该应用程序根据视频分辨率和方向使用自动信箱/邮筒全屏运行。

在 Chrome (Windows 7) 和 Node-Webkit 上,该应用程序运行良好,看起来不错且稳定。在 Android 4.0.3 (Galaxy Tab 10.1) 上,它运行良好 - 该应用程序看起来也不错且稳定,即使似乎无法预加载视频:我正在使用 javascript CreateElement("video ") 并将 preload 属性设置为 true)。在 Android 4.1.1 (Medion Lifetab) 上,视频没有加载,应用程序显示一个带有视频图标的灰色框。有谁知道为什么?

此外,“canplaythrough”事件似乎永远不会在平板电脑上触发(我猜是另一个错误),因此我被迫使用“loadstart”事件,它不一样但足够好。

这是源代码(一个从外部存储加载 html5 的简单 webview 查看器)

-------------------------------- activity_main.xml --------------- -----------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="horizontal"
    android:background="@null"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="true"
        android:scrollbars="none" />

</RelativeLayout>

-------------------------------- AndroidManifest.xml --------------- -----------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rr.pe2013"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:hardwareAccelerated="true"
        android:keepScreenOn="true"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rr.pe2013.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

-------------------------------- MainActivity.java --------------- -----------------

package com.rr.pe2013;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
    WebView webView;
    GestureDetector gestureDetector;

    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // data folder located on the sd card
        String webAppFolder = Environment.getExternalStorageDirectory().getPath() + "/rr/pe2013";

        // remove title bar as we already have it in the web app
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Point to the content view defined in XML
        setContentView(R.layout.activity_main);

        // configure the webview setup in the xml layout
        webView = (WebView) findViewById(R.id.webView);

        // webView.clearView();
        webView.setFocusable(true);
        webView.requestFocus();
        webView.setBackgroundColor(Color.BLACK);
        webView.setInitialScale(1);

        // web view settings
        WebSettings webSettings = webView.getSettings();

        // enable javascript, plugins, dom storage, file access
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setDomStorageEnabled(true);
        webSettings.setSaveFormData(false);
        webSettings.setSavePassword(false);
        webSettings.setAllowFileAccess(true);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            webSettings.setAllowContentAccess(true);
        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }

        // more settings
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setSupportMultipleWindows(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(false);
        webSettings.setSupportZoom(false);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            webSettings.setMediaPlaybackRequiresUserGesture(false);
        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            webSettings.setBuiltInZoomControls(false);
            webSettings.setDisplayZoomControls(false);
        }

        // webchrome client
        webView.setWebChromeClient(new WebChromeClient() {
            // handle javascript alerts
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
                showToast(message);
                result.confirm();
                return true;
            };

            // log console messages
            @Override
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                // showToast(sourceID + " [" + lineNumber + "]:\n" + message);
            }
            public boolean onConsoleMessage(ConsoleMessage cm) {
                showToast(cm.sourceId() + " [" + cm.lineNumber() + "]:\n" + cm.message());
                return true;
            }
        });

        // webview client
        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView webView, String url) {
            }

            @Override
            public void onReceivedError(WebView webview, int errorCode, String description, String failingUrl) {
                // showToast("onReceivedError: " + description);
            }
        });

        // load the URL
        String url = "file:///" + webAppFolder + "/index.html"; // from sd card

        webView.loadUrl(url);
    }

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

    @Override
    protected void onResume(){
        super.onResume();
        webView.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button 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);
    }

    // display an on-screen temporary message 
    public void showToast(String toast, int duration) {
        Toast.makeText(getApplicationContext(), toast, duration).show();
    }
    public void showToast(String toast) {
        showToast(toast, Toast.LENGTH_LONG);
    }
}
4

0 回答 0