Android Browser Development Example

Source: Internet
Author: User

Android app needs to display the Web page information on the phone or more commonly used, such as my recent amateur development of the Rob Shop games , need to explain the rules of the game, the rules will be more, and to constantly change, think of using Web pages to display, update up convenient, unlike the application, once released, It's hard to change.

To start with, the following is the introduction of my system webview use, do a simple browser, no picture no real image, first look at the Picture (blog Park can not be transmitted pictures, and other images to restore the function of re-transmission):

The first is the editing of the layout file Activity_browser.xml

Use relativelayout layout, do not know why use linearlayout layout not.

The browser title bar has three controls, button, TextView, button, to perform the return, display page titile, refresh operation.

Show WebView controls below the title bar

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"> <relativelayout android:layout_width="fill_parent"Android:layout_height="54DP"Android:id="@+id/title"> <!--navigation Bar left button--<Button Android:id="@+id/nav_left_btn"Android:layout_width="50dip"Android:layout_height="45dip"android:layout_centervertical="true"android:layout_gravity="center_vertical"Android:layout_marginleft="8DP"Android:layout_alignparentleft="true"android:gravity="Center"android:visibility="Invisible"> </Button> <!--navigation Bar text---<TextView Android:id="@+id/nav_titile_textview"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_centerhorizontal="true"android:layout_centervertical="true"android:gravity=""Android:text=""Android:textcolor="@color/white"android:textsize="20SP"/> <!--navigation Bar right Button-<Button Android:id="@+id/nav_right_btn"Android:layout_width="50dip"Android:layout_height="45dip"android:layout_centervertical="true"android:layout_gravity="center_vertical"Android:layout_alignparentright="true"Android:layout_marginright="8DP"android:gravity="Center"android:visibility="Invisible"> </Button></RelativeLayout> <WebView android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:layout_below="@id/title"Android:id="@+id/webview"/> </RelativeLayout>

and then the implementation part of the activity.

In the implementation of the WebView, there are 2 places to pay attention to

(1) If the page is in Chinese, GBK encoding, the default encoding value of WebView must be set

Webview.getsettings (). Setdefaulttextencodingname ("GBK");

(2) If the webpage has JS, must turn on JS, otherwise the display will have the problem. Especially for the HTML5 adaptation of the webpage, I use jquery Mobile to do a Web page, showing there is a problem. Just start to show only plain text, and then opened the JS Execute permission, it can be displayed normally.

Webview.getsettings (). Setjavascriptenabled (True);

---

Package Com.onevo.activity;import Android.app.activity;import Android.app.progressdialog;import Android.content.intent;import Android.graphics.bitmap;import Android.os.bundle;import android.text.TextUtils; Import Android.view.view;import Android.view.view.onclicklistener;import Android.webkit.webchromeclient;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.button;import  Android.widget.textview;import com.onevo.r;/** * Shows the first activity of tabhost, this activity mainly displays the device list * * @author Administrator * */public class Browseractivity extends Activity {private WebView webview;private progressdialog Mpgrogressdialog; Private TextView tv_title;private button btn_left;private button btn_right;private String url = "http://114.215.171.166/ level.html ";p ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (  R.layout.activity_browser); Gets the passed parameter intent it = Getintent (); String u = It.getstringextra ("url"); Textutils.isempty (U)) {url = u;} WebView = (WebView) This.findviewbyid (r.id.webview); tv_title = (TextView) This.findviewbyid (R.id.nav_titile_textview ); btn_left = (Button) This.findviewbyid (r.id.nav_left_btn); btn_right = (Button) This.findviewbyid (r.id.nav_right_btn ); btn_left.setvisibility (view.visible); btn_right.setvisibility (view.visible); Btn_left.settext ("return"); Btn_ Right.settext ("refresh");//Return Event Btn_left.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) { Finish ();}}); /Refresh Event Btn_right.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {mpgrogressdialog.show   (); Webview.loadurl (URL);}); /Displays the small chrysanthemum flower mpgrogressdialog in the webpage loading = new ProgressDialog (this); Mpgrogressdialog.settitle (""); Mpgrogressdialog.setmessage ("Loading Web page ..."); Mpgrogressdialog.show (); Webview.getsettings (). Setdefaulttextencodingname ("GBK");//Start Page load new thread in thread (new Runnable () {@Overridepublic void run () {Webview.loadurl (URL);}}).  Start ();  Webview.getsettings (). Setjavascriptenabled (True); Webview.setwebchRomeclient (New Webchromeclient () {@Overridepublic void Onreceivedtitle (WebView view, String title) {//TODO Auto-generated method Stubsuper.onreceivedtitle (view, title); final String tmptitle = Title;runonuithread (New Runnable (  {@Overridepublic void Run () {tv_title.settext (tmptitle);}});}  }); Webview.setwebviewclient (New Webviewclient () {public boolean shouldoverrideurlloading (WebView VI                     ew, String URL) {view.loadurl (URL);                 return true;                              } @Override public void onpagefinished (WebView view, String URL) {                                  super.onpagefinished (view, URL);                           Runonuithread (New Runnable () {@Overridepublic void run () {Mpgrogressdialog.dismiss ();});                  } @Override public void onpagestarted (WebView view, String URL, Bitmap favicon) { Super.onpagestarted (view, URL, favicon); }          }); }}

 

At this point, a simple inline browser has been developed.

If you use the WebView point link to see a lot of pages later, if you do not do any processing, click the System Back button, the entire browser will call the finish () method to end itself, if you want to browse the page fallback instead of exiting the browser, The back event needs to be processed and consumed in the current activity.


Overrides the onkeydown (int keycoder,keyevent event) method of the Activity class.
public boolean onKeyDown (int keycode,keyevent event) {
if (Webview.cangoback () && keycode = = keyevent.keycode_back) {
Webview.goback ();//GoBack () represents the previous page that returns WebView
return true;
}
return false;
}

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.