我使用android'WebView'加载了url内容。加载过程比在浏览器中加载URL更为耗时。
下面是我的代码:
actvitity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center">
<WebView
android:id="@+id/agreement_webView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layoutMode="opticalBounds"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView agreementWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
agreementWebView = (WebView)findViewById(R.id.agreement_webView);
loadAgreementWebView();
}
public void loadAgreementWebView(){
String url = "http://my-url/";
if (!url.equals("") || !url.isEmpty() || !url.equals("null")) {
WebSettings settings = agreementWebView.getSettings();
agreementWebView.setFocusable(true);
agreementWebView.setFocusableInTouchMode(true);
agreementWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
settings.setJavaScriptEnabled(true);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setAppCacheEnabled(true);
settings.setUserAgentString("Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
//settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
agreementWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
agreementWebView.setVisibility(View.VISIBLE);
CookieManager.getInstance().setAcceptCookie(true);
agreementWebView.setWebChromeClient(new WebChromeClient());
agreementWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("NEWS_WEB_VIEW", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.d("NEWS_WEB_VIEW", "Finished loading URL: " +url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("NEWS_WEB_VIEW", "Error: " + description);
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
// TODO Auto-generated method stub
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
public void onLoadResource(WebView view, String url)
{
Log.d("NEWS_WEB_VIEW", "onLoadResource" + view.getUrl()+" : "+url);
}
});
agreementWebView.loadUrl(url);
Log.d("NewsWebViewActivity","NEWS_URL: "+url);
} else {
Toast.makeText(this, "There is no URL to load", Toast.LENGTH_LONG).show();
}
}
}
AndroidMenifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
问题是,当在“ Webview”中加载页面(my-url)时,加载时间超过10秒。否则,在“浏览器”中加载页面(my-url)时,加载时间不到3秒。我需要做的是加载“ my-url”内容的速度比浏览器呈现的速度更快。
答案 0 :(得分:0)
在删除某些“ WebView”设置并插入此行之后
agreementWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
它很好加载。
已删除
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setAppCacheEnabled(true);
settings.setUserAgentString("Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
//settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
agreementWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
CookieManager.getInstance().setAcceptCookie(true);
agreementWebView.setWebChromeClient(new WebChromeClient());