1
votes

I have a WebView which displays a local HTML-File. In that HTML i've got a JavaScript Function hardcoded. Onclick from a Button also in that HTML-File the Function delivers to Strings to the JavaScriptInterface. From there i want to call a Method in my Activity with those two delivered Strings. Is this possible?

I've got set up everything and it's working but calling a Method in the Activity (from within the JSInterface) only works if i set the Activity Method to static and when it's set static, i can't call on other Methods in the Activity from within the static Method (that's triggered from the JSInterface).

Here's my Code, i hope you can help me:

public class JSWebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
JSWebAppInterface(Context c) {
    mContext = c;
}

@JavascriptInterface
public void sendData(String action, String filename){
    MbaiTextActivity.getDecision(action, filename);
}

}

public class MbaiTextActivity extends Activity {

private static final String TAG = "_TEXTACTIVITY";

MbaiWebView mwv;

RelativeLayout containerForWebViews;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_activity);

    containerForWebViews = (RelativeLayout) findViewById(R.id.webView_container);

    mwv = new MbaiWebView(this);
    mwv.setWebViewClient(new MbaiWebViewClient(this));
    mwv.setOnStatusListener(onWebViewListener);
    mwv.addJavascriptInterface(new JSWebAppInterface(this), "android");
    mwv.findViewById(R.id.webView_container);

    mwv.loadHTML(textsPath + storyparts.get(currentPosition).getFilename());
    containerForWebViews.addView(mwv);

private MbaiWebView.OnStatusListener onWebViewListener = new MbaiWebView.OnStatusListener()

static void getDecision(String action, String filename){
    // just for test purpose
    System.out.println("Aus der TextAct.: " + filename + " " + action);
    // do soemthing
}

}

From the JSInterface Class i called the test() in the Activity:

    public void test(String one, String two){
    System.out.println("Aus der Test: " + one + " " + two);
    mwv.loadHTML(textsPath + "derBrax_part5.html");
}


04-18 08:01:23.248 17751-17891/com.example.max.mybai I/System.out: Aus der Test: Springen derBrax_part1.html

04-18 08:01:23.249 17751-17891/com.example.max.mybai W/WebView: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {76eeef3} called on Looper (JavaBridge, tid 335) {6f228eb}, FYI main Looper is Looper (main, tid 1) {76eeef3}) at android.webkit.WebView.checkThread(WebView.java:2334) at android.webkit.WebView.loadUrl(WebView.java:927) at com.example.max.mybai.MbaiWebView.loadHTML(MbaiWebView.java:94) at com.example.max.mybai.MbaiTextActivity$override.test(MbaiTextActivity.java:180) at com.example.max.mybai.MbaiTextActivity$override.access$dispatch(MbaiTextActivity.java) at com.example.max.mybai.MbaiTextActivity.test(MbaiTextActivity.java:0) at com.example.max.mybai.JSWebAppInterface.sendData(JSWebAppInterface.java:24) at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.os.HandlerThread.run(HandlerThread.java:61) 04-18 08:01:23.249 17751-17891/com.example.max.mybai W/System.err: java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {76eeef3} called on Looper (JavaBridge, tid 335) {6f228eb}, FYI main Looper is Looper (main, tid 1) {76eeef3}) 04-18 08:01:23.249 17751-17891/com.example.max.mybai W/System.err: at android.webkit.WebView.checkThread(WebView.java:2344) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at android.webkit.WebView.loadUrl(WebView.java:927) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at com.example.max.mybai.MbaiWebView.loadHTML(MbaiWebView.java:94) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at com.example.max.mybai.MbaiTextActivity$override.test(MbaiTextActivity.java:180) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at com.example.max.mybai.MbaiTextActivity$override.access$dispatch(MbaiTextActivity.java) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at com.example.max.mybai.MbaiTextActivity.test(MbaiTextActivity.java:0) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at com.example.max.mybai.JSWebAppInterface.sendData(JSWebAppInterface.java:24) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at android.os.Looper.loop(Looper.java:154) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: Caused by: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {76eeef3} called on Looper (JavaBridge, tid 335) {6f228eb}, FYI main Looper is Looper (main, tid 1) {76eeef3}) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: at android.webkit.WebView.checkThread(WebView.java:2334) 04-18 08:01:23.250 17751-17891/com.example.max.mybai W/System.err: ... 11 more 04-18 08:01:23.255 17751-17751/com.example.max.mybai I/chromium: [INFO:CONSOLE(19)] "Uncaught Error: Java exception was raised during method invocation", source: file:///android_asset/books/der_brax/texts/derBrax_part1.html (19) 04-18 08:01:23.291 17751-17751/com.example.max.mybai W/art: Attempt to remove non-JNI local reference, dumping thread 04-18 08:01:23.432 17751-17751/com.example.max.mybai W/art: Attempt to remove non-JNI local reference, dumping thread

1

1 Answers

0
votes

Rewrite your code like so:

public void getDecision(String action, String filename){
    // just for test purpose
    System.out.println("Aus der TextAct.: " + filename + " " + action);
    // do soemthing
}

public class JSWebAppInterface {
    private final MbaiTextActivity act;

    /** Instantiate the interface and set the context */
    public JSWebAppInterface(MbaiTextActivity act) {
        this.act = act;
    }

    @JavascriptInterface
    public void sendData(String action, String filename){

        //EDIT:
        act.runOnUiThread(new Runnable() {

            @Override
            public void run () {

                 act.getDecision(action, filename);
            }
        });

    }
}

//in onCreate
mwv.addJavascriptInterface(new JSWebAppInterface(this), "android");