WebView Methods is not called in android

家住魔仙堡 提交于 2019-12-23 08:09:12

问题


My Web view is not calling the javascript function it is returning warning like below. Can anybody suggest how to get rid of the below warning.

07-30 10:15:44.031: W/webview_proxy(3770): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

Below is my function.

public boolean onLongClick(View v){
    System.out.println("dfdsf");
    // Tell the javascript to handle this if not in selection mode
    //if(!this.isInSelectionMode()){
        this.getSettings().setJavaScriptEnabled(true);
        this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        this.getSettings().setPluginsEnabled(true);
        this.loadUrl("javascript:android.selection.longTouch();");
        mScrolling = true;
        //this.setJavaScriptEnabled(true);
    //}


    // Don't let the webview handle it
    return true;
}

回答1:


The warning is telling you everything. You are calling the webview methods directly. That means you are calling them on WebViewCoreThread. You have to call them on the UI Thread that means in the Activity which uses this webview.

Like:

WebView wv = (WebView)findViewById(id);
wv.setJavaScriptEnabled(true);
wv.setJavaScriptCanOpenWindowsAutomatically(true);
wv.setPluginsEnabled(true);
wv.loadUrl("javascript:android.selection.longTouch();");



回答2:


As the warning says you are calling the webview methods in the WebViewCoreThread. Thus modify your code like this,

public boolean onLongClick(View v){
    YourActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            this.getSettings().setJavaScriptEnabled(true);
            this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            this.getSettings().setPluginsEnabled(true);
            this.loadUrl("javascript:android.selection.longTouch();");
            mScrolling = true;
        }
    });
}



回答3:


Use This Code I thinks it will work for you,And modified it according to your need ##

    private WebView webView;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        webView = (WebView) findViewById(R.id.web_view);
        webView.setInitialScale(1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.loadUrl("http://www.youtube.com");

    }

}



回答4:


onLongClick is a member of webview ?

It seems you can't call all WebView methods on thread 'WebViewCoreThread'.

You can use handler, send msg to handler in onLongClick , then call WebView methods in your handler.




回答5:


I think you must execute your code of onLongClick method in runOnUIThread() or using Handler, this warning is due to use all this on a worker thread.




回答6:


you can use WebView via Runnable. No Need to use Activity.

    webView.post(new Runnable()
    {
        @Override
        public void run()
        {
          getSettings().setJavaScriptEnabled(true);
          getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
          getSettings().setPluginsEnabled(true);
          loadUrl("javascript:android.selection.longTouch();");
          mScrolling = true;
        }
    });


来源:https://stackoverflow.com/questions/17944347/webview-methods-is-not-called-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!