簡體   English   中英

在Android 4.0.x中,WebView不會在應用程序啟動時呈現本地HTML頁面。Ice Cream Sandwich

[英]WebView does not render local HTML page on app launch in Android 4.0.x Ice Cream Sandwich

我知道在這個論壇上已經以不同的格式詢問了這個問題,但是到目前為止,我發現那里的答案都沒有-可以接受或以其他方式對我有幫助。

我正在使用本機android,HTML和Adboe的Flex SDK(沒有任何框架,例如PhoneGap等,使用Android自己的WebView的簡單代碼)的混合應用程序上工作。

這是我面臨的問題:

啟動應用程序時,首先會觸發flex sdk的活動“ AppEntry” ,這是一個空白活動,它僅為flex SDK設置了上下文和初始設置。 接下來,啟動本機MainActivity ,它使用WebView加載HTML項目。 在Android 4.0.x(ICS)上 ,即使認為已加載URL,Webview仍為空白(白色)(針對所涉及的URL成功調用了onPageFinished())。 在首次安裝和啟動該應用程序時會發生這種情況,在停止該應用程序后(通過將其從最近的應用程序欄中刪除),頁面將按預期加載。 如此反復重復啟動有時會重現該問題,但頻率不可預測。

需要考慮的一些事項:

  1. 由於flex sdk的限制,這些HTML文件不能直接存儲在'/ assets'文件夾中,而是存儲在資產文件夾中的目錄結構。

  2. 問題僅發生在冰淇淋三明治上(我猜是已知問題)

我已經嘗試過的東西:

  1. 硬件加速已關閉(開/關無關緊要,兩者均已測試)

2。

WebSettings settings = webView.getSettings();        
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSaveFormData(true);
        settings.setAllowFileAccess(true);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            settings.setAllowContentAccess(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                settings.setAllowFileAccessFromFileURLs(true);
                settings.setAllowUniversalAccessFromFileURLs(true);
            }
        }
        settings.setAppCacheEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        boolean enableZoom = true;
        settings.setBuiltInZoomControls(enableZoom);
        settings.setSupportZoom(enableZoom);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            initializePluginAPI(webView);
        }
        settings.setDatabaseEnabled(true);

問題是我在Play商店中取消了我的應用的評論評論。 任何幫助或見解將不勝感激。

即使認為已加載URL,Webview仍為空白(白色)(針對所討論的URL成功調用了onPageFinished())。

我通過使用mWebView.post()解決了

如果它僅高於4.0,我建議您向應用程序添加權限,因為Android需要此權限來加載資產文件,否則行為是間歇性的。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

在Android 3.x-4.0.x中,當URL包含查詢參數(?)或錨點(#)時,WebView在加載本地HTML時存在一個非常關鍵的錯誤。 直到Android 4.1才解決此問題。 您應該應用自定義WebView和WebViewClient。 是固定的WebView和WebViewClient。
您應該使用WebViewExWebViewClientEx而不是WebViewWebViewClient ,並且還應像下面那樣覆蓋WebViewClientEx 是Android項目成員的解決方法。

webViewEx.setWebViewClient(new WebViewClientEx(getContext()) {
    @Override
    public boolean shouldOverrideUrlLoadingEx(WebView view, String url) {
        boolean redirected = super.shouldOverrideUrlLoadingEx(view, url);

        if (!redirected) {
            if (url != null && URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                redirected = true;
            }
        }
        return redirected;
    }

    @Override
    public WebResourceResponse shouldInterceptRequestEx(WebView view, String url) {
        InputStream stream = inputStreamForAndroidResource(url);
        if (stream != null) {
            return new WebResourceResponse(null, null, stream);
        }
        return super.shouldInterceptRequestEx(view, url);
    }

    private InputStream inputStreamForAndroidResource(String url) {
        final String ANDROID_ASSET = "file:///android_asset/";

        if (url.startsWith(ANDROID_ASSET)) {
            url = url.replaceFirst(ANDROID_ASSET, "");
            try {
                AssetManager assets = getContext().getAssets();
                Uri uri = Uri.parse(url);
                return assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
            } catch (IOException e) {
            }
        }
        return null;
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM