パス変数stackoverflow post:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String key = "hello";
String val = "world";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("localStorage.setItem('"+ key +"','"+ val +"');", null);
} else {
webView.loadUrl("javascript:localStorage.setItem('"+ key +"','"+ val +"');");
}
}
});
第二の変形がJavaScriptInterface
Initセクション
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
JavaScriptInterface
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
public string getData(String someParameter){
//also you can return json data as string and at client side do JSON.parse
if (someParameter == "give me data" && this.activity.data) {
return this.activity.data;
}else{
return null;
}
}
}
のJSセクション
<script>
function ready() {
var data = window.JSInterface.getData("give me data");
localStorage.put("give me data", data)
};
document.addEventListener("DOMContentLoaded", ready);
</script>
[WebViewからHTMLページにJSON形式のデータを渡す方法](http://stackoverflow.com/questions/10114993/how-to-pass-json-formatted-data-from-a) -webview-to-a-html-page) – petey
データをWebビューに渡す必要があるのはなぜですか? – joshuamabina