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
Package com.android.webview.activity; Import android.app.Activity; Import Android.os.Bundle; Import android.view.KeyEvent; Import Android.webkit.WebView; public class Mainactivity extends Activity {private WebView WebView; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Instantiate the WebView object webview = new WebView (this); Set the WebView property to be able to execute JavaScript script webview.getsettings (). Setjavascriptenabled (True); Load the page webview.loadurl ("http://www.31358.cn/") that needs to be displayed; Set Web View Setcontentview (webview); } @Override//Set fallback/Overwrite activity class OnKeyDown (int keycoder,keyevent Event) method public boolean OnKeyDown (int KeyCode, KeyEvent event) {if ((keycode = = keyevent.keycode_back) && webview.cangoback ()) { Webview.goback (); GoBack () returns true for the previous page returning WebView; } 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=" 1.0 " > <uses-sdk android:minsdkversion= "ten"/> <application android:icon= "@drawable/icon" Android: Label= "@string/app_name" > <activity android:name= ". Mainactivity " android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application>
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)
Several ways Android will encapsulate HTML5 as an Android app apk file