Android browser development instance and android Development Instance

Source: Internet
Author: User

Android browser development instance and android Development Instance

Android apps need to display webpage information through mobile phones. For example, I recently developedShopaholicsYou need to describe the rules of the game. The rules will be many and need to be changed frequently. You can use web pages to display the rules, which is easy to update. Unlike applications, once published, it is difficult to change the rules.

Let's get down to the truth. Next we will introduce my use of webview in the system. We will make a simple browser without a picture or a real image. Let's take a look at the picture first. (Images cannot be uploaded in the blog garden. After the image function is restored, it will be uploaded again ):

 

 

The first step is to edit the layout file activity_browser.xml.

The RelativeLayout layout is used. I don't know why the LinearLayout layout is used.

The browser title bar has three controls: Button, TextView, And Button, which are used to perform the return operation, display the titile of the webpage, and refresh the webpage.

Display the webview control under 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"> <! -- Buttons on the left of the navigation bar --> <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"/> <! -- Button on the right of the navigation bar --> <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>

 

The following is the implementation part of the Activity. 

 

When implementing webview, note the following two points:

(1) If the webpage is Chinese and gbk encoded, the default encoding value of webview must be set.

 

WebView. getSettings (). setDefaultTextEncodingName ("gbk ");

 

(2) If the webpage has js, you must enable js, otherwise there will be problems with the display. In particular, for html5-adapted web pages, I use Jquery mobile for a web page, and there is a problem with the display. At the beginning, only plain text can be displayed. After the js execution permission is enabled, 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. butt On; import android. widget. textView; import com. onevo. r;/*** display the first Activity of tabhost. This activity mainly displays the list of devices ** @ 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"; public void onCreate (Bund Le savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_browser); // get the passed parameter Intent it = getIntent (); String u = it. getStringExtra ("url"); if (! 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) {finish () ;}}); // refresh event btn_right.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mPgrogressDialog. show (); webView. loadUrl (url) ;}}); // display the image of Chrysanthemum in the webpage loading mPgrogressDialog = new ProgressDialog (this); mPgrogressDialog. setTitle (""); mPgrogressDialog. setMessage ("loading webpage... "); mPgrogressDialog. show (); webView. getSettings (). setDefaultTextEncodingName ("gbk"); // start the web page to load new 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 view, 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 );}});}}

 

So far, a simple embedded browser has been developed.

If you use the WebView link to view many pages without any processing, click the Back key, and the entire browser will call the finish () method to finish itself, if you want to roll Back the web page instead of exiting the browser, you need to process and consume the Back event in the current Activity.


Override 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 () indicates that the previous page of webView is returned.
Return true;
}
Return false;
}


Help develop a simple Android program that calls the browser

This is very simple. In the future, there will be some code:
Uri uri = Uri. parse (url); // The url that you want to link
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );

If you have any questions, please contact me, my mailbox: yuzhenlong844@163.com

Android-based browser code Development System Using eclipse android

Drag the WebView control in the layout file to view the web content. You can set various attributes for it.

Add other controls (such as EditText as the input bar and Button refresh Button) to form a browser.
 

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.