More and more developers are keen to use Html5+javascript to develop mobile web apps. However, whether the advent of HTML5 Web apps will replace mobile applications in the future is still unknown. On the one hand, users in the use of habits, do not like to enter complex URLs on the browser, on the other hand, HTML5 Web App stored on the server side, in each use of the need for data transfer, resulting in a waste of traffic. Some developers do not want to touch complex Java code, then, what is the way to use HTML5 to develop the application, but also can be simply encapsulated as an apk file it?
the WebView
1 in the Android SDK. Instantiate the WebView component in the activity to: WebView WebView = new WebView (this);
2. Call WebView's Loadurl () method to set the page that Wevview want to display:
Internet use: Webview.loadurl ("http://www.31358.com");
Local files by: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
3. Call the activity's Setcontentview () method to display the page view
4. With WebView point link to see a lot of pages later in order to let WebView support fallback function, You need to overwrite the onkeydown () method that overrides the activity class, and if you don't do any processing, click the System Fallback button, and the entire browser calls finish () and ends itself instead of going back to the previous page,
5. You need to add permissions to the Androidmanifest.xml file, or the Web page not available error will occur.
<uses-permission android:name= "Android.permission.INTERNET"/>
Disadvantage: If the load is a normal web page, there is no problem, but if it is HTML5, encapsulated, in android2.3 above to normal access, android2.2 and below, the SDK WebView has not fully supported HTML5
Here's a concrete example:
Mainactivity.java
Packagecom.android.webview.activity; Importandroid.app.Activity; ImportAndroid.os.Bundle; Importandroid.view.KeyEvent; ImportAndroid.webkit.WebView; Public classMainactivityextendsActivity {PrivateWebView WebView; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //instantiating a WebView objectWebView =NewWebView ( This); //set the WebView property to be able to execute JavaScript scriptsWebview.getsettings (). setjavascriptenabled (true); //load Web pages that need to be displayedWebview.loadurl ("http://www.31358.cn/"); //set up a Web viewSetcontentview (WebView); } @Override//Set Fallback//onkeydown (int keycoder,keyevent event) method that overrides the activity class Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if((keycode = = Keyevent.keycode_back) &&Webview.cangoback ()) {Webview.goback ();//GoBack () indicates that the previous page of the WebView is returned return true; } return false; }
Add permissions in the Androidmanifest.xml file
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package =" Com.android.webview.activity " Android:versioncode = "1" android:versionname< /span>= "1.0" > <uses-sdk android:minsdkversion= "ten"/> <application android:icon= "@drawable/icon" Andro Id:label= "@string/app_name" > <activity android:name= ". Mainactivity " Android:label =" @string/app_name "> <i ntent-filter> <action android:name= "Android.intent.action.MAIN"/> <category A Ndroid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> < /application> <uses-permission android:name= "Android.permission.INTERNET"/> </manifest>
Second, the use of PhoneGap
PhoneGap is a fast-developing platform for creating mobile cross-platform mobile applications using HTML,CSS and JavaScript. It enables developers to take advantage of the core features of Iphone,android,palm,symbian,wp7,bada and BlackBerry smartphones-including geolocation, accelerators, contacts, sounds and vibrations, and PhoneGap has a rich plug-in, You can extend unlimited functionality with this. PhoneGap is free, but it requires additional software from a specific platform, such as the iphone sdk,android Android SDK for iphones, etc.
For detailed methods, see: Http://phonegap.com/start#android
Pros: Add SDK in Eclipse, programming freedom, perfect fit for different device screen size, suitable for master use.
Cons: Without using layouts, loading pages directly and not adding ads.
Third, the use of Rexsee online generation
Rexsee is an open source Android development platform that enables developers to standardize web development patterns and quickly implement mobile applications using HTML5, CSS3, and JavaScript. Will HTML will be Android. All you have to do is upload the good HTML5 application to the Rexsee server, and soon it will be compiled into the standard APK installation file.
Website: http://www.rexsee.com
Advantages: One-click Generation, suitable for ordinary people to use
Cons: Direct encapsulation, unable to add ads.
Iv. Appmobi Html5 XDK online generation (using the PhoneGap plugin)
http://www.appmobi.com/
This article is reprinted article, original address: http://mobile.51cto.com/android-386448.htm
If there is infringement please contact me can
Another way to do this is by using Hbuilder, which can be viewed in more detail: http://blog.csdn.net/uikoo9/article/details/43451377
Several ways to encapsulate HTML5 as an Android app apk file (reprint)