onPageFinished没有被触发

时间:2016-01-07 20:36:06

标签: android android-webview

为什么onProgressChangedonPageFinished都没有被触发?

我的webview没有任何错误(如谷歌浏览器的inspect中所示) 我在webview activity中的代码是:

我看到了this answer但是因为我刚接触到android编码无法弄清楚我是否犯了同样的错误。

有人可以帮忙纠正我的代码。

提前致谢

private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new TwitterCore(authConfig), new Digits());
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient());
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(this, "Android");
    mWebView.loadUrl("file:///android_asset/index.html");

}
public void onProgressChanged(WebView view, int progress){//not working
    if(progress==100) {
        SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
        String userPhone = settings.getString("userPhone", "null");
        Long userId = settings.getLong("userId", 0);
        String retVal = userPhone + "$" + userId;
        mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
    }
}
public void onPageFinished(WebView view, String url){//not workin
        SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
        String userPhone = settings.getString("userPhone", "null");
        Long userId = settings.getLong("userId", 0);
        String retVal = userPhone + "$" + userId;
        mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
}

1 个答案:

答案 0 :(得分:0)

感谢 @ 323go ,以下是答案(我只是希望我的任务不会被投票:))

onProgressChangedonPageFinished需要成为WebChromeClient的一部分。修改后的代码:

private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new TwitterCore(authConfig), new Digits());
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress){//not working
            if(progress==100) {
                SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
                String userPhone = settings.getString("userPhone", "null");
                Long userId = settings.getLong("userId", 0);
                String retVal = userPhone + "$" + userId;
                mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
            }
        }
        public void onPageFinished(WebView view, String url){//not workin
            SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
            String userPhone = settings.getString("userPhone", "null");
            Long userId = settings.getLong("userId", 0);
            String retVal = userPhone + "$" + userId;
            mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
        }
    });
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(this, "Android");
    mWebView.loadUrl("file:///android_asset/index.html");

}