0

I want a helper class to evaluate javascript with WebView:

public class EvaluateHelper {
    public static String evaluate(String st){
        WebView w;
        String result;
        //something code to evaluate javascript using WebView;
        return result;
    }
}

But the creation of web view needs a context, I want this class self-contained and do not want to modify other activity class to provide a context, I tried some methods:

  1. fulfil with null

    WebView w=new WebView(null);

  2. fulfil with new Activity

    WebView w=new WebView(new Activity());

  3. fulfil with context in new Application

    WebView w=new WebView(new Application().getApplicationContext());

  4. create custom child class from context, then provide my custom class for WebView:

    class MyContext extends Context{
        //some override method
    }
    
    WebView w=new WebView(new MyContext());
    

but they cause failed to run the app. Is there other way that create a WebView with my Context instead of app original Context? If not, is it possible to make this class self-contained?

ggrr
  • 7,737
  • 5
  • 31
  • 53

1 Answers1

2

You may want to take a look at this library from Square for running javascript

duktape-android from Square

Moh Mah
  • 2,005
  • 20
  • 29