Android uses WebView to pack Web pages into apps

Source: Internet
Author: User

The development cost of the native app is relatively high compared to the Web page, so more and more apps use Web pages as an interface, or even completely encapsulate a website as an app, which can improve the speed of development and basically achieve cross-platform.

The following is an example of Android, in the UBUNTU-14.04.4-DESKTOP-AMD64 environment to implement a simple WebView package website into the app process.

Environment preparation

The development environment requires the Java SDK (official website download), the Android SDK (official website download).

Java SDK Installation
wget http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jdk-8u91-linux-x64.tar.gz
Jdk-8u91-linux-x64.tar.gz

Then configure the path path and Java_home

VI ~/.BASHRC

At the end of the add

Export java_home=JDK extract directory export CLASSPATH=.: $CLASSPATH: $JAVA _home/libexport PATH= $PATH: $JAVA _ Home/bin

Save Exit VI, refresh configuration

SOURCE ~/.BASHRC
Android SDK Installation

PS: Ladders may be required during installation, please bring your own.

wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgztar -x-f Android-sdk_r24. 4.1-linux.tgzcd android-sdk-linux/tools

Let's see what SDK versions are available to install

./android List SDK

Then install the required SDK version

./android Update SDK--no-ui--filter use commas to separate serial numbers that need to be installed

Project creation and code writing

Create a new directory anywhere, save the project, and create a new SRC directory to hold the source files. Because Java has the concept of a package, so after entering the SRC directory, according to the level of the package name, set up the corresponding directory, and then create a new Java source program files, such as:

1  Packagetest.android;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 //import Android.app.AlertDialog;6 ImportAndroid.view.Window;7 //import Android.view.WindowManager;8 Importandroid.view.KeyEvent;9 ImportAndroid.webkit.WebView;Ten Importandroid.webkit.WebViewClient; One //import android.webkit.WebChromeClient; A //import Android.webkit.JsResult; - //import Android.content.DialogInterface; - //import Android.content.DialogInterface.OnClickListener; the  -  Public classMainextendsActivity { -    Public Static FinalString Load_url = "http://www.baidu.com/"; -  +   PrivateWebView WebView =NULL; -  +    Public voidonCreate (Bundle savedinstancestate) { A     Super. OnCreate (savedinstancestate); at  -     //this.requestwindowfeature (window.feature_no_title); -     //This.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_ fullscreen); -  -WebView =NewWebView ( This); -      This. Setcontentview (WebView); inWebview.setwebviewclient (Newwebviewclient () { -        Public voidonpagefinished (WebView view, String URL) { to         //webview.evaluatejavascript ("Test ();", null); +       } -     }); the     /* * webview.setwebchromeclient (New Webchromeclient () { $ Public Boolean Onjsalert (WebView view, string URL, string message, jsresult result) {Panax Notoginseng  - new Alertdialog.builder (_this) the . Setmessage (message) + . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () { A Public void OnClick (dialoginterface dialog, int which) {} the }). Show (); +  - return true; $       } $     }); -     */ -Webview.getsettings (). setjavascriptenabled (true); theWebview.addjavascriptinterface ( This, "native"); - Webview.loadurl (load_url);Wuyi   } the  -   //Overwrite Back button Wu    Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { -     if(Keyevent.keycode_back = = KeyCode &&Webview.cangoback ()) { About Webview.goback (); $       return true; -     } -  -     return Super. OnKeyDown (KeyCode, event); A   } +  the}

Save the file as Main.java

Go back to the project root and create another file, save as Androidmanifest.xml, with the following:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Test.android">3   <ApplicationAndroid:icon= "@drawable/icon"4 Android:label= "@string/app_name">5     <ActivityAndroid:name=". Main ">6       <Intent-filter>7         <ActionAndroid:name= "Android.intent.action.MAIN" />8         <categoryAndroid:name= "Android.intent.category.LAUNCHER" />9       </Intent-filter>Ten     </Activity> One   </Application> A </Manifest>

Create a new res directory at the project root and create a new drawable and values directory within Res.

Create a new XML file Strings.xml within values as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>  <stringname= "App_name">Resource Testing</string>  <stringname= "Web_url">Http://www.baidu.com</string></Resources>

Then copy the required program icon into the drawable directory with the file name Icon.png

Compile

Switch to the project directory first.

To compile the resource first, create the Gen directory at the project root, save the generated R.java resource number, and enter the following command in the console:

/opt/android-sdk-linux/build-tools/24.0. 0package-f-m-j gen-s res-i/opt/android-sdk-linux/platforms/android-/aapt/android.jar-m Androi Dmanifest.xml

When compiling the Java source file, add the R.java source file:

Javac-encoding utf-8-source 1.6-target 1.6-bootclasspath/opt/android-sdk-linux/platforms/android-24/android.jar-d Bin/classes Src/test/android/main.java Gen/test/android/r.java

Android uses WebView to pack Web pages into apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.