我仍然是编程的初学者,目前我的代码在我的webview中,我有一个带有JavaScript代码的onPageFinished,用于从我的webview中检索纯文本。但onPageFinished将始终在我的主线程之后完成,之后我无法继续处理其他任何事情。
无论如何,我可以检查等待onPageFinished完成或延迟我的主线程,以便它不会在我的onPageFinished之前结束?我迫切需要解决方案。
这是我的代码。
WebViewActivity
类
public class WebViewActivity extends Activity{
String[] paraText;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webview = (WebView) findViewById(R.id.webView);
FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.button_voice);
//Retrieve value from CustomAdapter
Intent intent = getIntent();
String address = intent.getStringExtra("URL");
//Enable JavaScript
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(this,"myjava");
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
/*view.loadUrl("javascript:(function() { " + "document.getElementById('mk-navbar-secondary').style.display=\"none\"; " + "})()");
view.loadUrl("javascript:(function() { " + "document.getElementsByClassName('uk-list uk-clearfix uk-margin-bottom-remove').style.display=\"none\"; " + "})()");
view.loadUrl("javascript:(function() { " + "document.getElementsByTagName('section')[0].style.display=\"none\"; " + "})()");
view.loadUrl("javascript:(function() { " + "document.getElementsByTagName('section')[1].style.display=\"none\"; " + "})()");
view.loadUrl("javascript:(function() { " + "document.getElementsByTagName('section')[3].style.display=\"none\"; " + "})()");*/
view.loadUrl("javascript:var paraTexts = []");
view.loadUrl("javascript:var count = document.getElementsByTagName('p');");
view.loadUrl("javascript:var loop;");
view.loadUrl("javascript:for(loop = 0; loop < count.length; loop++){ paraTexts.push( document.getElementsByTagName('p')[loop].innerHTML); }");
view.loadUrl("javascript:myjava.onData(paraTexts);");
Log.d("OUTPUT TEST","onPageFinished");
}
});
webview.loadUrl(address);
}
//This method will be registered as a JavaScript interface
@JavascriptInterface
@SuppressWarnings("unused")
public void onData(String[] value){
this.paraText = value;
Log.d("OUTPUT TEST onDATA",value[0]);
Log.d("OUTPUT TEST onDATA.P",paraText[0]);
//new SpeakText(getApplicationContext(),paraText,floatingActionButton).execute();
}
public void onDataDisplay(){
Log.d("OUTPUT TEST disDATA",paraText[0]);
}
}
答案 0 :(得分:0)
好吧,因为我找到了自己的解决方案;
如上所述here,让UI线程进入睡眠是不可能完成的,因为它会暂停内部的所有活动。所以使用处理程序postDelay代替。
解决方案代码:
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
// do something
}
}, 3000);