How to pass parameter into HTML file from android

Viewed 12622

I can show up HTML file content in android webview well.Now how could i pass parameter into HTML file.For ex.my HTML content has an video player i need to pass dynamic values(URL) into HTML file for playing dynamic video.My HTML file is located on asset folder.How could i do this?

Thanks.

5 Answers

I managed to pass variables in a different way.

My problem was that everytime I switched to another app, when coming to the webapp, the webview kept reloading. I guess that's because of the following line in my onCreate() method: myWebView.loadUrl(url); I had the idea to pass these state variables in the url, but as you know it is not possible yet. What I did was to save the state of some variables using onSaveInstanceState(Bundle outState) {...} and restore them with onRestoreInstanceState(Bundle savedInstanceState){...}.

In onCreate method after setting up myWebView I did the following:

myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String urlString)
{
     Log.i("onPageFinished", "loadVariables("+newURL+")");
     if(newURL!="")
         myWebView.loadUrl("javascript:loadVariables("+"\""+newURL+"\")");
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}
});

jsInterface = new JSInterface(this,myWebView);
myWebView.addJavascriptInterface(jsInterface, "Android");

if (savedInstanceState != null)
{
// retrieve saved variables and build a new URL
newURL = "www.yoururl.com";
newURL +="?var1=" + savedInstanceState.getInt("key1");
newURL +="?var2=" + savedInstanceState.getInt("key2");
Log.i("myWebApp","NEW URL = " + newURL);
}
myWebView.loadUrl("www.yoururl.com");

So, what it happens is that first I load the page with the default URL (www.yoururl.com) and onPageFinished I call a new javascript method where I pass the variables. In javascript loadVariables function looks like this:

function loadVariables(urlString){
    // if it is not the default URL
    if(urlString!="www.yoururl.com")
    {
        console.log("loadVariables: " + urlString);
        // parse the URL using a javascript url parser (here I use purl.js)
        var source = $.url(urlString).attr('source');
        var query = $.url(urlString).attr('query');  
        console.log("URL SOURCE = "+source + " URL QUERY = "+query);
        //do something with the variables 
    }
}
Related