我的應用程序首先將網頁載入onCreate作爲WebView。我應該把它擴展到一個新的線程,因爲它可能會掛起一點?或者至少有一種方式顯示該頁面仍在加載。它偶爾會顯示爲白色幾秒鐘。WebView載入頁面Android
此外,有沒有辦法來防止方向更改頁面重新加載?
我的應用程序首先將網頁載入onCreate作爲WebView。我應該把它擴展到一個新的線程,因爲它可能會掛起一點?或者至少有一種方式顯示該頁面仍在加載。它偶爾會顯示爲白色幾秒鐘。WebView載入頁面Android
此外,有沒有辦法來防止方向更改頁面重新加載?
webview處理線程本身,所以你不必擔心這一點。
您可以在頁面啓動並完成加載時註冊回調。你需要建立一個進度條,或者你想要的任何東西。詳情請參閱WebChromeClient.onProgressChanged()。這是一個good post,它提供了一些細節。
你可以添加一些東西到你的清單告訴系統你不關心方向的變化。以下添加到您的活動定義,
android:configChanges="orientation"
的另一種選擇是你的應用程序鎖定到一個方向或另一個,
android:screenOrientation="portait"
廣告1. I have not (and should not) do that in separate thread.
WebView
進度條 - Android WebView progress bar
廣告2.可以阻止方向通過增加android:screenOrientation="portait"
或"landscape"
中體現你的Activity
的聲明改變。
您應該使用進度對話框。顯示對話框,直到將Web頁面加載到WebView。
public final class MyWebview extends Activity {
private static final int DIALOG2_KEY = 1;
private ProgressDialog pd = null;
private static final String AmitBlog="YOUR URL";
private WebView webView;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
pd = new ProgressDialog(this);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setBuiltInZoomControls(true);
/** Showing Dialog Here */
showDialog(DIALOG2_KEY);
}
@Override
protected void onResume() {
super.onResume();
LoadView();
}
private void LoadView()
{
webView.loadUrl(AmitBlog);
}
/** Its very important while navigation hardware back button if we navigate to another link .Its like a Stack pop of all the pages you visited in WebView */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/** WebViewClient is used to open other web link to the same Activity. */
private final class HelpClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
/** Dismissing Dialog Here after page load*/
dismissDialog(DIALOG2_KEY);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
return false;
} else{
view.loadUrl(url);
return true;
}
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id)
{
case DIALOG2_KEY:
{
pd.setMessage(getResources().getString(R.string.Loading));
pd.setIndeterminate(true);
pd.setCancelable(false);
return pd;
}
}
return null;
}
}