How to execute JavaScript on Android?

后端 未结 10 1440
迷失自我
迷失自我 2020-11-27 14:38

I have code which uses ScriptEngineManager, ScriptEngine class for executing JavaScript code using Java. But it works fine in Java SE, and doesn\'t work in Android - SDK sh

10条回答
  •  无人及你
    2020-11-27 14:53

    You can use Webview which inherits View class. Make an XML tag and use findViewById() function to use in the activity. But to use the JavaScript, you can make a HTML file containing the JavaScript code. The example blelow might help.

    Webview browser=(Webview) findViewById(R.main.browser); //if you gave the id as browser
    browser.getSettings().setJavaScriptEnabled(true); //Yes you have to do it
    browser.loadUrl("file:///android_asset/JsPage.html"); //If you put the HTML file in asset folder of android
    

    Remember that the JS will run on WebView, not in native environment, thus you might experience a lag or slow FPS in emulator. However when using on an actual phone, the code may run fast, depending on how fast is your phone.

提交回复
热议问题