【问题标题】:Android webview is slow in below 5.0(lollipop) but in 5.0 it running fineAndroid webview 在 5.0(棒棒糖)以下运行缓慢,但在 5.0 运行良好
【发布时间】:2015-09-08 11:53:42
【问题描述】:

我在 5.0(棒棒糖)以下的 android 中加载 url 时遇到问题 加载数据太慢,但在 android 5.0 或更高版本中可以正常工作。如何解决这个问题? I have follow this link 。 & How to improve webview load time

我的代码是

   @SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
      public class YS_WebViewActivity extends Activity
     {
    private Button btnDone;
    private WebView webViewLoadUrl;
    private YS_GeneralUtility generalUtility;
    private Dialog alertDialogBox;
    private String url = "";
    private TextView tvLoading;
    private Button btnCancel;
    private Button btnRetry;

    @SuppressLint("InlinedApi")
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        btnDone = (Button) findViewById(R.id.btnDone);
        webViewLoadUrl = (WebView) findViewById(R.id.webViewLoadUrl);
        //webViewLoadUrl.setBackgroundColor(Color.parseColor("#123456"));
        tvLoading = (TextView) findViewById(R.id.tvLoading);
        url = getIntent().getExtras().getString("url");
        webViewLoadUrl.setVisibility(View.INVISIBLE);
        generalUtility = new YS_GeneralUtility(this);

        webViewLoadUrl.getSettings().setJavaScriptEnabled(true); // enable javascript
        try {
            webViewLoadUrl.getSettings().setRenderPriority(RenderPriority.HIGH); 
            webViewLoadUrl.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webViewLoadUrl.getSettings().setDomStorageEnabled(true);
            webViewLoadUrl.getSettings().setAllowFileAccess(true);
            webViewLoadUrl.getSettings().setAllowContentAccess(true);
            webViewLoadUrl.getSettings().setAllowFileAccessFromFileURLs(true);
            //webViewLoadUrl.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvLoading.setTypeface(generalUtility.faceAvenirLight);
        btnDone.setTypeface(generalUtility.faceAvenirMedium);
        btnDone.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                myErrorFinish();
            }
        });
        webViewLoadUrl.setWebViewClient(new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                //Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url)
            {
                //Log.i(TAG, "Finished loading URL: " + url);
                webViewLoadUrl.setVisibility(View.VISIBLE);
                tvLoading.setVisibility(View.INVISIBLE);
            }

            String errorData = "<html><head></head>"
                    + "<body style=' font-family:Avenir !important;font-weight:lighter !important; color:#ffffff; margin : 0px auto;'>"
                    + "<div style = 'width : 100% ; height : 100% ; background:#123456;'> </div>"
                    + "</body></html>";

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
            //  Log.e(TAG, failingUrl + "Error: " + description);
                view.loadData(errorData, "text/html", "UTF-8");
                alertDialogOnInternetOff(getString(R.string.errorMsgInternet));
                // finish();
            }
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });

        webViewLoadUrl.loadUrl(url);
        // setContentView(webview);
    }


    private void alertDialogOnInternetOff(String message)
    {
        alertDialogBox = new Dialog(YS_WebViewActivity.this, R.style.InternetErrorPopup);
        alertDialogBox.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialogBox.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialogBox.setContentView(R.layout.internetpopup_dialog);
        alertDialogBox.setCancelable(false);
        alertDialogBox.show();
        btnCancel = (Button) alertDialogBox.findViewById(R.id.btnCancel);
        btnRetry = (Button) alertDialogBox.findViewById(R.id.btnRetry);
        TextView txtExitMessage = (TextView) alertDialogBox.findViewById(R.id.textViewMessage);
        TextView horizontalLinePopup = (TextView) alertDialogBox.findViewById(R.id.horizontalLinePopup);
        TextView verticalLinePopup = (TextView) alertDialogBox.findViewById(R.id.verticalLinePopup);
        horizontalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
        verticalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
        txtExitMessage.setTypeface(generalUtility.faceAvenirLight);
        btnCancel.setTypeface(generalUtility.faceAvenirLight);
        btnRetry.setTypeface(generalUtility.faceAvenirLight);
        txtExitMessage.setText(R.string.errorMsgInternet);
        btnCancel.setText(R.string.cancel);
        btnRetry.setText(R.string.retry);
        alertDialogBox.setCancelable(false);
        btnCancel.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                alertDialogBox.dismiss();
                Log.d("AlertDialog", "Negative");
                YS_WebViewActivity.this.finish();
            }
        });
        btnRetry.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialogBox.dismiss();
                Log.d("AlertDialog", "Positive");
                tvLoading.setVisibility(View.VISIBLE);
                webViewLoadUrl.loadUrl(url);

            }
        });
    }
    private void myErrorFinish() 
    {
        /*
         * Jump to the Setting Screen on pressing back button of device
         */
        YS_WebViewActivity.this.finish();
        //overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
    }

    @Override
    public void onBackPressed() 
    {
        myErrorFinish();
        super.onBackPressed();
    }
  }

这是 XML...

     <RelativeLayout 
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/listback"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="42dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/navigationbackground" />

    <Button
        android:id="@+id/btnDone"
        android:layout_width="wrap_content"
        android:layout_height="42dip"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent"
        android:text="@string/done"
        android:textColor="@android:color/white"
        android:textSize="17sp" />

    <WebView
        android:id="@+id/webViewLoadUrl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btnDone"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/tvLoading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/loading"
        android:textColor="@android:color/white"
        android:textSize="18sp" />


    </RelativeLayout>

【问题讨论】:

  • 你检查我的答案了吗
  • 先生,这不起作用感谢您的回答...

标签: android webview android-webview


【解决方案1】:

在 Android 4.4(2013 年 10 月)中,Google 切换了其WebView to use the Chromium rendering engine — 此举带来了显着的速度提升、众多新 API 以及使用 Chrome DevTools 进行远程调试的能力。

根据Google’s documentation,他们在 Android 5.0 中更新了一些附加功能,为 webview 带来了更高的性能。

Android 5.0 的初始版本包括一个 Chromium 版本,用于 基于 Chromium M37 版本的 WebView,增加了对 WebRTC 的支持, WebAudio 和 WebGL。

Chromium M37 还包括对所有 Web 的原生支持 组件规范:自定义元素、Shadow DOM、HTML 导入、 和模板。这意味着您可以使用 Polymer 及其材料设计 WebView 中的元素,无需 polyfill。

虽然 WebView 自 Android 4.4 以来一直基于 Chromium,但 Chromium 层现在可以从 Google Play 更新。

因此,根据 webview 的高端变化,与低版本相比,它会更有效地加载数据。

对于您的问题,您可以根据自己的情况加快 android webview 的性能,例如 Android webview slowAndroid WebView performanceAndroid webview loading data performance very slow

Hardware Acceleration vll 是诀窍。您可以在此answer 中的应用程序的不同级别中使用它,我也提到了其他类型。

我认为以下方法效果最好:

if (Build.VERSION.SDK_INT >= 19) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}       
else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

希望这个答案对你有所帮助。

【讨论】:

    【解决方案2】:

    如果在所有其他答案之后您的问题仍然存在,那么您需要测试您的 URL/服务器响应和数据格式

    【讨论】:

    • 感谢 MPG.. 响应是愚蠢的错误,这就是为什么不显示数据。
    【解决方案3】:

    试试

    webview.getSettings().setRenderPriority(RenderPriority.HIGH);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    

    【讨论】:

    • 我已经尝试过了,但是没有用。上面我已经给出了这个链接,如果可能的话,请让我知道另一个选项。谢谢你的回复。
    • 另一种方式是 Webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    • setRenderPriority 现已弃用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多