How to use ScriptEngineManager in Android?

2020-07-20 02:17发布

import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

 public void bEqual(View v) throws ScriptException {

       ScriptEngineManager mgr = new ScriptEngineManager();
       ScriptEngine engine = mgr.getEngineByName("JavaScript");
        String value = inputText.getText().toString();
        Toast.makeText(this,value,Toast.LENGTH_LONG).show();
        try{
            result = (double)engine.eval(value);
            Toast.makeText(this,String.format("%f",result),Toast.LENGTH_SHORT).show();
        }catch(Exception e){
            Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
        }

    }

What is wrong in it? App is exiting when perform this action. It is not showing any errors but app is closing

5条回答
我只想做你的唯一
2楼-- · 2020-07-20 02:26

javax.script not available in android that's why you have to use third party library

I am using "rhino-android" and it serve the purpose

https://github.com/APISENSE/rhino-android

implementation in gradle file

implementation 'io.apisense:rhino-android:1.0'

java code

javax.script.ScriptEngine engine = new javax.script.ScriptException().getEngineByName("rhino");
Object result = engine.eval("2+2");
查看更多
成全新的幸福
3楼-- · 2020-07-20 02:38

Easiest way is to use Mozilla Rhino in Android instead[1] because ScriptEngine and it's dependencies would require time consuming setup and headache.

Import the ff:

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import android.util.Log;

Code you put somewhere in your method:

    Context context = Context.enter(); // 
    context.setOptimizationLevel(-1); // this is required[2]
    Scriptable scope = context.initStandardObjects();
    Object result = context.evaluateString(scope, "18 > 17 and 18 < 100", "<cmd>", 1, null);
    Log.d("your-tag-here", "" + result);

Add it in your gradle dependencies.

implementation group: 'org.mozilla', name: 'rhino', version: '1.7.10'

Reference:

  1. Mozilla Rhino
  2. Explanation why that line of code is needed
  3. Mozilla Rhino's Maven Repository
查看更多
我想做一个坏孩纸
4楼-- · 2020-07-20 02:41

first add this: compile 'io.apisense:rhino-android:1.0' to your app gradle file. Then type this snippet code

    TextView resultTextView = findViewById(R.id.result_text_view);
    String currentText = resultTextView.getText().toString();
    boolean valid = checkForValidity();
    double result=0;
    if(valid){
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
        try{
            result = (double)engine.eval(currentText);
        }catch(Exception e){
            Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
        }
        currentText = currentText +"\n"+ "="+(result);
    }
    resultTextView.setText(currentText);
查看更多
【Aperson】
5楼-- · 2020-07-20 02:41

This is a real example using Rhino Script Engine in Android in Android

Use this imports:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

and this is an example:

           String Operation = "5+4-2";
           ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

            try {
               Object result = engine.eval(Operation);
                Log.d("Calculator", "Operation: " + Operation + " result: " + result);
            } catch (ScriptException e) {
                Log.d("Calculator", " ScriptEngine error: " + e.getMessage());
            }

Output:

Operation: 5+4-2 result: 7.0
查看更多
Explosion°爆炸
6楼-- · 2020-07-20 02:47

The javax.script package is not available on Android.

If you want to use a Javascript engine, you must implement those behaviours by yourself or use one of the dependencies that already exist like rhino-android.

查看更多
登录 后发表回答