我正在开发一个应用程序,其中我在Webview中加载来自外部URL的网页。 加载页面需要很长时间,需要30秒到1分钟的时间才能加载完成。 请查看我的代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView)findViewById(R.id.webView);
progressBar= (ProgressBar)findViewById(R.id.progressBar2);
String link = getIntent().getExtras().getString("webLink");
String title = getIntent().getExtras().getString("webTitle");
setTitle(title);
webView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
Log.d("WEB", link);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//webView.getSettings().setDomStorageEnabled(true);
//webView.getSettings().setAppCachePath(String.valueOf(getCacheDir()));
//webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(link);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
}
}
有什么想法可以提高性能吗?