Android-webview Basic Use

Source: Internet
Author: User

Small series has been the task of combining the Web and Android components to do the application can be more effective, HTML5 one more convincing, especially for the former web development Brothers

1. WebView Join the layout file:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/allpage"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

<webview android:id= "@+id/webview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
</WebView>
</RelativeLayout>

2. Rewrite webchromeclient or WebClient, mainly for the cue box and the return key

public class Webbrowserclient extends Webchromeclient {
String title = Constants.appname;
@Override
public void Onclosewindow (WebView window) {
Super.onclosewindow (window);
}

@Override
public boolean Oncreatewindow (WebView View, Boolean dialog,
Boolean usergesture, Message resultmsg) {
Return Super.oncreatewindow (View, dialog, Usergesture, resultmsg);
}

/**
* Override the default Window.alert display interface to prevent the title from appearing as ": From file:////"
*/
Public Boolean Onjsalert (WebView view, string URL, string message, jsresult result) {
Final Alertdialog.builder Builder = new Alertdialog.builder (View.getcontext ());

Builder.settitle (title). Setmessage (Message). Setpositivebutton ("OK", null);

No need to bind key events
Shielding KeyCode equal to 84 keys
Builder.setonkeylistener (New Onkeylistener () {
public boolean OnKey (dialoginterface dialog, int keycode,keyevent event) {
LOG.V ("Onjsalert", "keycode==" + keycode + "event=" + event);
return true;
}
});
Disable response to events that press the back key
Builder.setcancelable (TRUE);
Alertdialog dialog = Builder.create ();
Dialog.show ();
Result.confirm ();//Because there is no binding event, you need to force confirm, otherwise the page will be black to display content.
return true;
Return Super.onjsalert (view, URL, message, result);
}

3. The definition in the HTML page:

function Gopage (URL) {
WinDow.androidMain.loadURL (URL);
}

4. Use of activity

Initializing a Web page
private void Setupviews () {
Mwebview.sethorizontalscrollbarenabled (FALSE);
Mwebview.setverticalscrollbarenabled (TRUE);
Mwebview.setscrollbarfadingenabled (TRUE);
Mwebview.setwebchromeclient (New Webbrowserclient ());
Mwebview.setinitialscale (1);
Mwebview.setlongclickable (FALSE);

WebSettings mwebsettings = Mwebview.getsettings ();
Mwebsettings.setjavascriptenabled (TRUE);
Mwebsettings.setusewideviewport (TRUE);
Mwebsettings.setloadwithoverviewmode (TRUE);
Mwebsettings.setusewideviewport (TRUE);


Multi-Touch Zoom support
Mwebsettings.setsupportzoom (FALSE);
Mwebsettings.setbuiltinzoomcontrols (FALSE);

Using buffering
Mwebsettings.setcachemode (websettings.load_cache_else_network);
Mwebview.loadurl ("file:///android_asset/b.html");

Objectclasshere cc = new Objectclasshere ();
mwebview.addjavascriptinterface (CC, "Androidmain");
}

Class Objectclasshere {

public void Exitsystem () {
Finish ();
Android.os.Process.killProcess (Android.os.Process.myPid ());
}

public void Loadurl (String URL) {
String furl = "file:///android_asset/" + url;
Mwebview.loadurl (Furl);
}

public void Showchapinadvs (int cr) {
LOG.V (Tag, "111 Showchapinadvs currlevel=" +CR% 2);
if ((cr% 2) = = 0) {
Showadvs = true;
Spotmanager.getinstance (context). Showspotads (context);
}
}

5. Web page development cycle and effect are faster than the activity, but the current effect is relatively general, needs to be improved

There are many use scenes, such as blowing bubble "http://zhushou.360.cn/detail/index/soft_id/341284", bubble animation with activity is difficult to achieve, but it is easier to use HTML+JS, The combination of activtiy control of the microphone can be done.


Android-webview Basic Use

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.