2

I am developing an app where i need to send one method request from javascript to android native and need to implement the code in native side. My javascript file consisits of.

 function _CBSubscribeForNative( eventName, Message, Data ) {
            try {                                                  
               Android.CallFromJavaScript(eventName,Message.data);                    
               }//...

In Main.java file i am using the following code.

 {
    WebView wv = (WebView)  findViewById(R.id.webView1);        
    WebSettings webset = wv.getSettings();        
    webset.setJavaScriptEnabled(true);   
    wv.loadUrl("file:///android_asset/DocApt/DocApt/DocAptApp/72/index.html");
    wv.addJavascriptInterface(new AndroidBridge(), "Android");
   }//oncreate

  private class AndroidBridge {
    @SuppressWarnings("unused")
    public void CallFromJavaScript(final String arg , final String arg1) {
        System.out.println("222222222");
    handler.post(new Runnable() {
    public void run() {
        String requestfrmjs = arg.toString();
        Toast.makeText(getApplicationContext(), "received request is " + requestfrmjs, Toast.LENGTH_SHORT).show();
    }       
    });
    }
    }

As per my code i am unable to get toast. Is there anything went wrong with my code.

can anyone please help me with this..

1 Answer 1

2

The second parameter is the name by which you will access, in the javascript context, the object you pass in first parameter.

So yes, you will need to use that name later if you want to call some method or property of that object from the javascript code.

See the docs for addJavascriptInterface() .

Sign up to request clarification or add additional context in comments.

2 Comments

For example, given your code, in JavaScript, you can call android.AN_CallFromJavaScript("foo", "bar");, which will be processed by your AndroidBridge class.
as per your answer the code i am working is correct or not. Here in log also it is not displaying the data which i get from javascript...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.