为什么onProgressChanged
和onPageFinished
都没有被触发?
我的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 + "')");
}
答案 0 :(得分:0)
感谢 @ 323go ,以下是答案(我只是希望我的任务不会被投票:))
onProgressChanged
和onPageFinished
需要成为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");
}