Hybrid Development Overview
, Weibo and now a lot of software use embedded H5 page, some outsourcing companies, in order to save costs, the use of Android embedded H5 model developed to facilitate the direct reuse of the page on iOS, so as to improve development efficiency.
the principle of implementation:
- The essence is: Java code and JavaScript calls
- H5 page, just HTML extension, JavaScript used to handle the logic of the page
Android and H5 intermodulation case: I. Java call JSBasic settings for WebView:
Private void Initwebview() {WebView =NewWebView ( This); WebSettings websettings = Webview.getsettings ();//Set support JavaScript Footstep languageWebsettings.setjavascriptenabled (true);//Support double click-if the page is to be supported before display //websettings.setusewideviewport (true); //Support Zoom button-provided the page is supported for displayWebsettings.setbuiltinzoomcontrols (true);//Set client-do not jump to the default browserWebview.setwebviewclient (NewWebviewclient ());//Set support JS call JavaWebview.addjavascriptinterface (NewAndroidandjsinterface (),"Android");//Load Local resourcesWebview.loadurl ("File:///android_asset/JavaAndJavaScriptCall.html");//Display page//Setcontentview (WebView);}
Java Call JS principle is the Java code called JS inside the function.
core Java code snippet:
//登录功能里,java代码调用了js里面的JavaCallJs函数实现将name传到JS中,这样JS页面可以显示该用户名了。 privatevoidlogin(String name) { webView.loadUrl("javascript:javaCallJs(""‘""‘"")"); setContentView(webView); }
Core JS Code snippet:
<script type="text/javascript"> function javaCallJs(arg){ document.getElementById("content").innerHTML = ("欢迎:"+arg ); } </script>
two. JavaScript tune Java
1. Configure the JavaScript interface in the Initialize WebView code:
//设置支持js调用java,调用时候将执行第一个参数的接口类 webView.addJavascriptInterface(new AndroidAndJSInterface(),"Android");//第二个参数为执行接口类方法的标示,与js相呼应
2. Implement the JavaScript interface class in this activity:
/** * js可以调用该类的方法 */ class AndroidAndJSInterface{ @JavascriptInterface publicvoidshowToast(){ Toast.makeText(JavaAndJSActivity.this"我被js调用了", Toast.LENGTH_SHORT).show(); } }
The core code snippet for calling Java code in 3_javascript:
type="button"value="点击Android被调用" onclick="window.Android.showToast()" />
Execution process:
Click on the JS page button to execute the JS onclick method (onclick= "window. Android.showtoast () "), which matches the second parameter of the WebView configuration interface method according to the Android label, and then executes the JS interface implementation class's Showtoast () method. This enables the JavaScript code to call Java code.
H5 page call Android Play video
The basic idea is the same as the above example (the execution process ), the business logic level to play the video is mainly from the H5 page click Play Jump (the video playback of the URL passed to the Java layer) and trigger the Java implementation of the code to play the video.
1. Core configuration in WebView:
//设置支持js调用java webView.addJavascriptInterface(new AndroidAndJSInterface(),"android");
2. Implement the JavaScript interface class in this activity:
class AndroidAndJSInterface { /** * 该方法将被js调用 * @param id * @param videoUrl * @param tile */ @JavascriptInterface publicvoidplayVideo(int id,String videoUrl,String tile){ //调起系统所有播放器 new Intent(); intent.setDataAndType(Uri.parse(videoUrl),"video/*"); startActivity(intent); } }
The core code snippet for calling Java code in 3.JavaScript:
javascript:android.playVideo(itemidvideourlitemtitle);
H5 Page calls Android phone call
The idea is the same, click on the H5 page to upload the phone number to the Java layer and call the dial core code.
Implement the JavaScript interface class in this activity:
@JavascriptInterface publicvoidcall(String phone) { new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)); if (ActivityCompat.checkSelfPermission(JsCallJavaCallPhoneActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { return; } startActivity(intent); }
Key Content
1. If you call the following method when the error, then the JavaScript interface class can be added to the specific implementation of the annotated:@JavascriptInterface, or change the targetsdkversion to 16 (generally not applicable)
//设置支持js调用java webView.addJavascriptInterface(new"Android");
2. Do not forget to add the Androidmanifest.xml to the application.
Primary Android combined with H5 Hybrid development summary