我仍然是编程的初学者,目前我有用于Webview的代码,我的onPageFinished内部带有JavaScript代码,可从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]);
}
}

最佳答案

好吧,因为我找到了自己的解决方案;

here所述,使UI线程进入睡眠状态是绝不应该做的事情,因为它将暂停内部的所有活动。因此,改为使用处理程序postDelay。

解决方案代码:

 Handler handler = new Handler();
 handler.postDelayed(new Runnable(){
  @Override
  public void run(){
  // do something
    }
  }, 3000);

10-07 15:21