...
->Compose received information fragments into one javascript code
js_url = intent.getStringArrayExtra("js_url");
playfunc = intent.getStringExtra("playfunc");
for (int i = 0;i < js_url.length;i++) {
String funcPtr = "";
if (js_url[i] == null) {
break;
}
if (i == 0) {
funcPtr = playfunc;
}
js_url_string = js_url_string +
" var script=document.createElement('script'); "
+ " script.setAttribute('type','text/javascript'); "
+ " script.setAttribute('src', '"
+ js_url[i]
+ "'); "
+ " script.onload = function() {"
+ funcPtr + ";"
+ "};"
+ " document.getElementsByTagName('head')[0].appendChild(script); ";
}
System.out.println(js_url_string);
->Create a WebView not used for display but executing javascript code
WebView myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
System.out.println(android.os.Build.VERSION.SDK_INT);
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(function() { "
+ js_url_string
+ "})()");
}
});
->Create a JS-Android Interface name
myWebView.addJavascriptInterface(new MyAndroidJSInterface(this, SecondActivity.class), "atv");
->As a activation key for starting off running javascript later
myWebView.loadUrl("file:///android_asset/tmp.html");
沒有留言:
張貼留言