在onCreate

时间:2016-08-04 18:03:56

标签: javascript java android webview

目前我有一个WebView onPageFinished,它包含检索值的JavaScript代码。出于某种原因,JavaScript代码总是在onCreate完成后完成检索。因此,使任何可靠的检索值的代码返回null错误。让我无法使用检索数据。我对这个问题感到沮丧。我一直在寻找高低解决方案,但无济于事。

这是我的班级WebViewActivity

 public class WebViewActivity extends Activity{

String[] paraText;
WebView webview;
FloatingActionButton floatingActionButton;
TextToSpeech textToSpeech;


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);

    webview = (WebView) findViewById(R.id.webView);
    floatingActionButton = (FloatingActionButton) findViewById(R.id.button_voice);
    textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.UK);
            }
        }
    });


    //Retrieve value from CustomAdapter
    Intent intent = getIntent();
    final String address = intent.getStringExtra("URL");

        webViewLoadURL(address);



}

//An instance of this class will be registered as a JavaScript interface
@JavascriptInterface
@SuppressWarnings("unused")
public void onData(String[] value){
    this.paraText = value;
    Log.d("Output Test: Value :",value[0]);
    Log.d("Output Test: P.Text :",paraText[0]);

}

public void webViewLoadURL(String address){



    webview.getSettings().setJavaScriptEnabled(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        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) {

            Log.d("Output Test:"," shouldOverrideUrlLoading");
            return super.shouldOverrideUrlLoading(view, request);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.d("Output Test:"," onPageStarted");

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:"
                    + "var paraTexts = [];"
                    + "var count = document.getElementsByTagName('p');"
                    + "var loop;"
                    + "for(loop = 0; loop < count.length; loop++){"
                    + "paraTexts.push(document.getElementsByTagName('p')[loop].innerHTML);"
                    +"}"
                    + "myjava.onData(paraTexts);");

            view.loadUrl("javascript:console.log('ParaText Disply' + paraTexts.toString());");
            Log.d("Output Test:"," onPageFinished");

        }
    });

    webview.loadUrl(address);

    testOutput();

}

public void testOutput(){
    Log.d("Output Test:"," Print");
}

}

0 个答案:

没有答案