Android WebView加载屏幕

时间:2013-12-01 19:04:58

标签: android web-applications webview

我想在加载网站时添加加载圈弹出窗口,我还想在应用程序中上传/下载文件,但我不知道做类似的事情。它是一个简单的WebView功能,但我不知道如何完成这项工作......

package net.schwarzis.htl_cloud;

import android.os.Bundle;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("JavascriptInterface")
public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private WebView browser = null;

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        browser.loadUrl(url);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // connect to our browser so we can manipulate it
        browser = (WebView) findViewById(R.id.mybrowser);

        // get settings so we can config our WebView instance
        WebSettings settings = browser.getSettings();

        // JavaScript?  Of course!
        settings.setJavaScriptEnabled(true);
        // clear cache
        browser.clearCache(true);

        browser.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                // do your stuff here
            }
        });
        // this is necessary for "alert()" to work
        browser.setWebChromeClient(new WebChromeClient());

        // add our custom functionality to the javascript environment
        browser.addJavascriptInterface(new MyCoolJSHandler(), "Cloud");

        // load a page to get things started
        browser.loadUrl("http://cloud.schwarzis.net");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (browser.canGoBack() == true) {
                        browser.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    final class MyCoolJSHandler {
        // write to LogCat (Info)
        public void Info(String str) {
            Log.i("GoingNative", str);
        }

        // write to LogCat (Error)
        public void Error(String str) {
            Log.e("GoingNative", str);
        }

        // Kill the app
        public void EndApp() {
            finish();
        }
    }
}

    ` 

1 个答案:

答案 0 :(得分:1)

您可以在致电之前启动进度对话框:

ProgressDialog pd=new ProgressDialog(this);
pd.show();

browser.loadUrl("http://cloud.schwarzis.net");

并在

中将其解雇
mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
       pd.dismiss();
    }
});

编辑:

final ProgressDialog pd=new ProgressDialog(this);
    browser.setWebViewClient(new WebViewClient() {
                public void onPageFinished(WebView view, String url) {
                     pd.dismiss();
                }
            });
            // this is necessary for "alert()" to work
            browser.setWebChromeClient(new WebChromeClient());

            // add our custom functionality to the javascript environment
            browser.addJavascriptInterface(new MyCoolJSHandler(), "Cloud");

            // load a page to get things started
 pd.show();
            browser.loadUrl("http://cloud.schwarzis.net");

修改-2

 browser.setWebViewClient(new WebViewClient() {
                    public void onPageFinished(WebView view, String url) {
                         pd.dismiss();
                    }
               @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                pd.show();
                return true;
            }
                });

修改-3

 final ProgressDialog pd=new ProgressDialog(this);
   pd.setTitle("Whatever you want to set");