1

I want to access a webapp as a android app, for this what I have done is simply calling url of that webapp by using webview. Webapp contains one js variable, I want to access that variable into androids MainActivity, how can I access and assign value to js variable which is in different webapp by using android MainActivity. Following is my code snippet for MainActivity,

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mWebview = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setDatabaseEnabled(true);
        mWebview.getSettings().setLoadWithOverviewMode(true);

        mWebview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        mWebview.setScrollbarFadingEnabled(false);
        mWebview.loadUrl("http://www.mywebapp.com");

        setContentView(mWebview);


    }

My main question is how to access and assign a value to js variable through androids MainActivity which is in different app/html?

1

1 Answer 1

3
     // Call method in js or html
 Android.getImageUrl("text");

    // add this line in main activity.    
     webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

    // js value get in this method.(add this method in activity)
    public class JavaScriptInterface {
                Context mContext;

                JavaScriptInterface(Context c) {
                    mContext = c;
                }

                @JavascriptInterface
                public void getImageUrl(String text) {
        // js value get here.
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

3 Comments

where should I implement getImageUrl(); method ?
you can call this method in js or html
Yes I will call it from js/html, but where is getImageUrl() method definition is implemented? In mainActivity or somewhere else? Should I have to implement it or not?? I am sorry asking very basic questions I am new to android.

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.