最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android webview - JavaScript callback not working? - Stack Overflow

programmeradmin1浏览0评论

I am using a WebView to display a web-page on an android application. .html

After clicking a button from the HTML, I can successfully get to the Android class WebAppInterface (From the example) and display a "Toast" alert, but trying to callback to the a javacsript function defined in my web-page is not working.

This is the code of the web-page: (Android.html)

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        if (typeof Android != 'undefined') 
            Android.showToast(toast);       


    }

    function ChangeColor()
    {
        document.body.style.background = 'pink';
    }
</script>

This is the code of the MainActivity of the Android app. It loads the url and displays it.

public class MainActivity extends AppCompatActivity {

    WebView m_Webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_Webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = m_Webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        m_Webview.setWebViewClient(new WebViewClient());
        m_Webview.loadUrl("android.html");


        WebView webView = (WebView) findViewById(R.id.webview);
        webView.addJavascriptInterface(new WebAppInterface(this,webView), "Android");
    }
}

This is the WebAppInterface on the Android app:

public class WebAppInterface {

    Context mContext;
    WebView mView;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c,WebView w) {
        mContext = c;
        mView = w;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        mView.loadUrl("javascript:ChangeColor()");
    }
}

The JavaScript function ChangeColor is not getting called after the toast is getting displayed.

For more reference see:

Thanks!

I am using a WebView to display a web-page on an android application. https://developer.android./guide/webapps/webview.html

After clicking a button from the HTML, I can successfully get to the Android class WebAppInterface (From the example) and display a "Toast" alert, but trying to callback to the a javacsript function defined in my web-page is not working.

This is the code of the web-page: (Android.html)

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        if (typeof Android != 'undefined') 
            Android.showToast(toast);       


    }

    function ChangeColor()
    {
        document.body.style.background = 'pink';
    }
</script>

This is the code of the MainActivity of the Android app. It loads the url and displays it.

public class MainActivity extends AppCompatActivity {

    WebView m_Webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_Webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = m_Webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        m_Webview.setWebViewClient(new WebViewClient());
        m_Webview.loadUrl("android.html");


        WebView webView = (WebView) findViewById(R.id.webview);
        webView.addJavascriptInterface(new WebAppInterface(this,webView), "Android");
    }
}

This is the WebAppInterface on the Android app:

public class WebAppInterface {

    Context mContext;
    WebView mView;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c,WebView w) {
        mContext = c;
        mView = w;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        mView.loadUrl("javascript:ChangeColor()");
    }
}

The JavaScript function ChangeColor is not getting called after the toast is getting displayed.

For more reference see: https://stackoverflow./a/14145116/7751339

Thanks!

Share Improve this question edited Sep 24, 2017 at 19:08 David Somekh asked Sep 24, 2017 at 18:50 David SomekhDavid Somekh 9633 gold badges14 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The solution was to use the "post" function like this:

public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
      //  WebView Web = new WebView(mContext);
       // mView.getSettings().setUserAgentString("Android WebView");
        mView.post(new Runnable() {
            public void run() {
                mView.loadUrl("javascript:ChangeColor()");
            }
        });
    }

Also on KitKat and forward you need to use evaluateJavascript:

mView.evaluateJavascript("ChangeColor();",null);
发布评论

评论列表(0)

  1. 暂无评论