Hybrid App Development: Using WebView to Load Pages

Hybrid App Development: Using WebView to Load Pages

Hybrid App is the abbreviation of hybrid mode application, which has the advantages of both Native App and Web App, with low development cost and cross-platform characteristics of Web technology. Currently, all the middleware-based mobile development frameworks we know of adopt the Hybrid development model, such as PhoneGap , Titanium, Sencha from abroad, and AppCan, Rexsee, etc. from China. The Hybrid App development model is being recognized by more and more companies and developers, and I believe it will become the mainstream mobile application development model in the future.

The principle of Hybrid App fusion Web App is to embed a WebView component, in which you can load pages, which is equivalent to an embedded browser. The code is as follows:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.webkit.WebSettings;
  4. import android.webkit.WebView;
  5.   
  6. public   class AActivity extends Activity{
  7.       
  8. @Override  
  9. public   void onCreate(Bundle savedInstanceState) {
  10. super .onCreate(savedInstanceState);
  11. // Create a WebView  
  12. WebView webView= new WebView( this );
  13. // Switch to the content view  
  14. setContentView(webView);
  15. // Get WebView configuration  
  16. WebSettings ws = webView.getSettings();
  17. // Enable JavaScript  
  18. ws.setJavaScriptEnabled( true );
  19. // Load a page in the assets directory  
  20. webView.loadUrl( "file:///android_asset/www/BoBox/index.html" );
  21. }
  22. }

Another way to introduce it is to add the WebView component in the layout file. The code is as follows:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3. android:orientation = "vertical"   
  4. android:layout_width = "fill_parent"   
  5. android:layout_height = "fill_parent" >   
  6.        
  7. < WebView    
  8. android:layout_width = "fill_parent"   
  9. android:layout_height = "wrap_content"   
  10. android:id = "@+id/webview"   
  11. />   
  12.            
  13. </LinearLayout>
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.webkit.WebSettings;
  4. import android.webkit.WebView;
  5.   
  6. public   class BActivity extends Activity{
  7.   
  8. @Override  
  9. public   void onCreate(Bundle savedInstanceState) {
  10. super .onCreate(savedInstanceState);
  11. setContentView(R.layout.webview);
  12. // Find WebView  
  13. WebView webView = (WebView) findViewById(R.id.webview);
  14. // Get WebView configuration  
  15. WebSettings ws = webView.getSettings();
  16. // Enable JavaScript  
  17. ws.setJavaScriptEnabled( true );
  18. // Load a page in the assets directory  
  19. webView.loadUrl( "file:///android_asset/www/index.html" );
  20. }
  21. }

WebView also has a very important method - addJavascriptInterface, which can be used to implement mutual calls between Java programs and JavaScript programs. The code is as follows:

  1. webView.addJavascriptInterface( new Object(){
  2. public   void clickOnAndroid(){
  3. mHandler.post( new Runnable(){
  4. public   void run(){
  5. webView.loadUrl( "javascript:wave()" );
  6. }
  7. });
  8. }
  9. }, "demo" );

The page code is as follows:

  1. < script >  
  2. function wave() {
  3. document.getElementById("id") .innerHTML = "Hello World!" ;
  4. }
  5. </ script >  
  6. </ head >  
  7. < body >  
  8. < div >  
  9. <   href = "#"   id = "demo"   onclick = "window.demo.clickOnAndroid()" > Click Me </ a >  
  10. </ div >  
  11. </ body >  
  12. </ html >  

In this way, when you click the Click Me button on the page, the clickOnAndroid function in the Java code will be called, and the clickOnAndroid function will call the wave method in the page. It should be noted that this interface will cause WebView to crash when running in the Android 2.3 version of the emulator, and it has not been fixed yet. This is a very simple example of demonstrating the mutual calls between Java and JavaScript. In actual applications, the clickOnAndroid function called by the page can call device functions such as camera, address book, notification reminder, etc.

<<:  Tingyun CTO: AWS and Tingyun join hands to create a domestic cloud + APM model

>>:  Frameworks and tools that hybrid app developers must not miss

Recommend

Can disposable masks protect against the sun? Be careful of getting darker!

If there is one thing that has suddenly appeared ...

17 thoughts on marketing, copywriting, and life!

1. Please accept this marketing trick! Frederick ...

IDC room rental high bandwidth cost

Most of the bandwidth at home and the data traffi...

Android Unified Push Alliance list released: Google participates as an observer

According to a report on December 11, the Unified...

Can eating konjac help you lose weight? Pay attention to these two points!

01 Why is konjac so low in calories? Konjac has a...

Can eating more tomatoes help whiten your skin? Tomato: I can’t do that~

gossip Tomatoes are a common food in our daily li...

Where is the beauty of Hoh Xil?

Hoh Xil Located in the western part of Yushu Tibe...

Optimization example of Baidu bidding in the machinery industry

In October, I took over the Baidu bidding account...