Android + HTML development mobile app DEMO code

Source: Internet
Author: User

Recently, I have been engaged in mobile projects. To manage mobile project development in the future, I have to learn something about it, and I have to reverse my work on Android.

Because the technology selection uses the phonegap + native plug-in, I learned how to use phonegap. I always feel that phonegap will depend too much on it in the future. After all, phonegap is a new project, and many of the effects are native, finally, the project chose completely native development, and several Nb-level people were added to the group. However, there are still a lot of interface details needed to see their complex application interfaces.ProgramThe adjustment is time-consuming and labor-consuming. In particular, the front-end interface changes are even more painful. Therefore, I think of HTML5 as an interface. After all, HTML5 is fast, and the front-end can be completed in a one-stop manner, but I don't want to use phonegap, so I have my own demo solution.

Do not be picky about the UI of this demo. I will not design the UI, but I just have some ideas, which are implemented in combination with native and HTML5.

The general goal is to communicateAlgorithm, All are placed on the native layer, and the main interface is displayed to HTML5. Data exchange is completed through JS intercommunication. In this way, the communication interface can be unified to facilitate integration.

Every page generates an activity. The excessive effect is a native interface, but you do not need to develop many activities, but one or more are enough, as shown below:Code:Code download

Activity: webview. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android ="Http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Webview
Android: Id = "@ + ID/webview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: scrollbarstyle = "insideoverlay">
</Webview>
</Linearlayout>

Unified activity code:

Public class accountactivity extends basewebviewactivitiy {
@ Override
/** Called when the activity is first created .*/
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Thisactivity = this;
Setcontentview (R. layout. webview );
Initwebview (R. Id. webview );

Intent myintent = getintent ();
String page = myintent. getstringextra ("page ");
Setupwebview (PAGE );
}

Private void setupwebview (string page ){
Webview. addjavascriptinterface (New javascriptinterface (), "Caller ");
If ("regist". Equals (page )){
Loadurl (webview, "file: // android_asset/account/regist.html ");
} Else if ("forgetpassword". Equals (page )){
Loadurl (webview, "file: // android_asset/account/forgetpassword.html ");
} Else {
Isroot = true;
Loadurl (webview, "file: // android_asset/account/login.html ");
}

}
Private class javascriptinterface {
@ Suppresswarnings ("UNUSED ")
Public void regist (){
Intent myintent = new intent (accountactivity. This, accountactivity. Class );
Myintent. putextra ("page", "regist ");
Accountactivity. This. startactivity (myintent );
}
@ Suppresswarnings ("UNUSED ")
Public void forgetpassword (){
Intent myintent = new intent (thisactivity, accountactivity. Class );
Myintent. putextra ("page", "forgetpassword ");
Accountactivity. This. startactivity (myintent );
// Toast. maketext (activitythis, tip, Toast. length_short). Show ();
}
@ Suppresswarnings ("UNUSED ")
Public void returnlast (){
Accountactivity. This. Finish ();
}
@ Suppresswarnings ("UNUSED ")
Public void login (){
Userstatus. getinst (). setprofile (New USERPROFILE ());
Intent myintent = new intent (accountactivity. This, useractivity. Class );
// Myintent. putextra ("page", "home ");
Accountactivity. This. setresult (result_ OK );
Accountactivity. This. Finish ();
}
}
}

Base activity:

Public class basewebviewactivitiy extends activity {
Protected webview = NULL;
Protected handler mhandler = new handler ();
Protected activity thisactivity;
Private progressdialog PD;
Protected Boolean isroot = false;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
This. setrequestedorientation (activityinfo. screen_orientation_portrait );
}

Protected void initwebview (INT webview ){
Webview = (webview) findviewbyid (webview );
Mhandler = new handler (){
Public void handlemessage (Message MSG)
{// Define a handler for processing the communication between the download thread and the UI
If (! Thread. currentthread (). isinterrupted ())
{
Switch (msg. What)
{
Case 0:
PD. Show (); // displayed progress dialog box
Break;
Case 1:
PD. Hide (); // hide the progress dialog box. Dismiss () and cancel () cannot be used. Otherwise, when show () is called again, the small circle of the displayed dialog box does not change.
Break;
}
}
Super. handlemessage (MSG );
}
};
Webview. getsettings (). setjavascriptenabled (true );
Webview. setscrollbarstyle (view. scrollbars_inside_overlay );
Webview. setwebviewclient (New webviewclient (){
Public Boolean shouldoverrideurlloading (final webview view, final string URL ){
Loadurl (view, URL); // load a webpage
Return true;
} // Rewrite the click action and load it with webview

});

Webview. setwebchromeclient (New webchromeclient (){
Public void onprogresschanged (webview view, int progress) {// triggered when the loading progress changes
If (Progress = 100 ){
Mhandler. sendemptymessage (1); // hide the progress dialog box if all data is loaded.
}
Super. onprogresschanged (view, progress );
}
});
Pd = new progressdialog (thisactivity );
PD. setprogressstyle (progressdialog. style_spinner );
PD. setmessage ("loading data. Please wait! ");

}

Public Boolean onkeydown (INT keycode, keyevent event) {// capture the return key
If (keycode = keyevent. keycode_back) & webview. cangoback ()){
Webview. Goback ();
Return true;
} Else if (keycode = keyevent. keycode_back ){
Confirmexit (); // if the return key is pressed but the return key cannot be returned, exit and confirm.
Return true;
}
Return super. onkeydown (keycode, event );
}
Private void confirmit () {// exit confirmation
If (! Isroot ){
Thisactivity. Finish (); // close the activity
} Else {
Alertdialog. Builder ad = new alertdialog. Builder (thisactivity );
Ad. settitle ("quit ");
Ad. setmessage ("Exit software? ");
Ad. setpositivebutton ("yes", new dialoginterface. onclicklistener () {// exit button
Public void onclick (dialoginterface dialog, int I ){
Thisactivity. Finish (); // close the activity
}
});
Ad. setnegativebutton ("no", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int I ){
// No operation is required if you do not exit
}
});
Ad. Show (); // display dialog box
}
}
Protected void loadurl (final webview view, final string URL ){
New thread (){
Public void run (){
Mhandler. sendemptymessage (0 );
View. loadurl (URL); // load the webpage
}
}. Start ();
}
}

JavascriptInteractive code:

// JavaScript document
$ (Function (){
$ ("# Btnreturnlogin"). BIND ("click", function (){
If ($. OS. Android ){
Window. Caller. returnlast ();
//} Else if ($. OS. iOS ){
} Else {
Window. Open ("login.html", "_ Self ");
}
});
$ ("# Btnregist"). BIND ("click", function (){
If ($. OS. Android ){
Window. Caller. regist ();
//} Else if ($. OS. iOS ){
} Else {
Window. Open ("regist.html", "_ Self ");
}
});
$ ("# Btnforget"). BIND ("click", function (){
If ($. OS. Android ){
Window. Caller. forgetpassword ();
//} Else if ($. OS. iOS ){
} Else {
Window. Open ("forgetpassword.html", "_ Self ");
}
});
$ ("# Btnlogin"). BIND ("click", function (){
If ($. OS. Android ){
Window. Caller. login ();
//} Else if ($. OS. iOS ){
} Else {
Window. Open ("../user/payout.html", "_ Self ");
}
});
});

Related Article

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.