I went through all the posts shared in here, I still couldn't find my proper answer to run my code and make my app functional. I'll appreciate it if any of you who could help me on this issue.
I am calling bounded function getLocation() in tages as mentioned: . The getLocation function is bounded to MyHandler in Android as mentioned in below:
public class MainActivity extends Activity {
WebView webView;
private Handler mHandler = new Handler ();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface (new Object(){
public void getLocation(){
mHandler.post (new Runnable(){
public void run(){
String[] values = {"3.16802","101.71309"};
webView.loadUrl("javascript:setLocation("+values+")");
}
});
}
}, "MyHandler");
webView.loadUrl("file:///android_asset/example/quick-start-example.html");
}
}
The function setLocation in Android calls the method in JavaScrip as below, here we are passing the array into JS function:
function setLocation(val){
var lat = val[0];
var lng = val[1];
}
Please advice on this. Thank you.