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

Optimizing the paid experience is the key to Android TV games

In 2014, as relevant state departments relaxed re...

How can newbies make money through online promotion?

The Internet is a very magical world. As long as ...

The Weird Science and Art Behind Fighting Culture

Leviathan Press: Few people would say that they l...

How to correctly place ads on TikTok?

1 Behind the TikTok phenomenon As of mid-June 201...

Windows 10's entry into the mobile field may pose a threat to Android?

Microsoft made several big announcements recently...

Convincing "deep fakes" just to make you smile?

Leviathan Press: I personally think Walter Schere...