Android app embedded WAP mobile Web site example

Source: Internet
Author: User

Our embedded Android app phone site, m.baidu.com, cannot display the title bar, click on the hyperlink to the page, and will not eject the system with its own browser.


Layout Web.xml creates a new layout file web.xml under the layouts directory, as follows

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<webview android:id= "@+id/webview" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_weight= "1.0" >
</webview></linearlayout>

The layout file was written, so we started writing code, and I copied the entire code to the following

Package cn.afcu.sdk;

Import Android.os.Bundle;
Import Android.view.View;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;

public class Web extends Mainactivity {

Private WebView WebView;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.web);
WebView = (webview) Findviewbyid (R.id.webview);
Openbrowser ();

Webview.setwebviewclient (New Webviewclient () {
public boolean shouldoverrideurlloading (webview view, String URL)
{//rewrite This method to indicate that clicking on the link in the page or in the current WebView jump, do not jump to the browser there
View.loadurl (URL);
return true;
}
});
}

The Loadurl method of using WebView
public void Openbrowser () {
Webview.setscrollbarstyle (View.scrollbars_outside_overlay)//scroll bar appears inside WebView
Webview.setscrollbarstyle (View.scrollbars_inside_overlay)//scroll bar displayed on WebView outside
Webview.setverticalscrollbarenabled (FALSE); Vertical does not show
WebSettings websettings = Webview.getsettings ();
Websettings.setjavascriptenabled (TRUE);
Webview.loadurl ("http://www.baidu.com");
}
}



Andriod uses WebView controls to embed Web pages into apps

1. Layout file fragment: Res-layout

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

2.Java Fragment: src

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

WebView
WebView browser= (WebView) Findviewbyid (R.id.toweb);
Browser.loadurl ("http://www.baidu.com");

Set up a Web page to scale freely
Browser.getsettings (). Setsupportzoom (True);
Browser.getsettings (). Setbuiltinzoomcontrols (True);

If the link in the page, if you want to click on the link to continue in the current browser response,
Instead of the new Android system browser should be linked, must overwrite WebView webviewclient object
Browser.setwebviewclient (New Webviewclient () {
public boolean shouldoverrideurlloading (webview view, String URL)
{
Override this method to indicate that clicking on the link inside the page or in the current WebView jump, do not jump to the browser side
View.loadurl (URL);
return true;
}
});
}

Go back
@Override
public boolean onKeyDown (int keycode, keyevent event) {
WebView browser= (WebView) Findviewbyid (R.id.toweb);
Check If the key event is the back button and if there ' s history
if ((keycode = = keyevent.keycode_back) && browser.cangoback ()) {
Browser.goback ();
return true;
}
return true;
If It wasn ' t the back key or there ' No. Web page history, bubble up to the default
System behavior (probably exit activity)
Return Super.onkeydown (KeyCode, event);
}

3. Androidmanifest.xml set permissions, otherwise cannot access

<!--Add this permission, WebView can access the Web page-->
<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>

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.