Android - How to use Javascript in an Android WebView?

大兔子大兔子 提交于 2019-12-06 05:29:10
Sagar

Based on the Binding JavaScript documentation

Note: The object that is bound to your JavaScript runs in another thread and not in the thread in which it was constructed.

You are starting the activity on non-UI thread. You should run it on UI thread as follows:

@android.webkit.JavascriptInterface
    public void changeActivity(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 Intent i = new Intent(MainActivity.this, JavascriptInterfaceActivity.class);
                 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                 startActivity(i);
                 finish();
            }
        });

    }

Alternatively you can also use Handler.

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