For Android? WebView Loading Baidu Homepage

Source: Internet
Author: User

To load the homepage of the homepage, you can use WebView to implement. WebView (network view) can load the display page, we can use it to do the browser load Web page, the following day before yesterday with WebView implementation of Web page loading method.

WebView uses the WebKit rendering engine to load the Display Web page, there are two ways to implement the WebView:
The first type:
1. Instantiate the WebView component in activity: WebView Mwebview = new WebView (this);
2. Call WebView's Loadurl () method to set the page to display Wevview:
Internet such as Google with: Mwebview.loadurl ("http://www.google.com");
For local files: Mwebview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
3. Call the activity's Setcontentview () method to display the page view
4, in the WebView Web page to open a connection to return to the original page, you can override the Activity class OnKeyDown () method, if you do not do any processing, click on the phone's own return button, the browser will call finish () close the browser, Instead of going back to the previous page
5, to access the network also need to add permissions in the Androidmanifest.xml file, or a Web page not available error will occur.

< uses-permission android:name="android.permission.INTERNET" />The specific code is as follows
Mainactivity.java
Package cn.wuxiaocheng.browser;  import android.app.activity;import android.os.bundle ;import android.view.keyevent;import android.webkit.webview;  public class  mainactivity extends activity {     webview mwebview;       @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         mwebview = new webview (This);                         //instantiates the WebView object         mwebview.getsettings (). Setjavascriptenabled (TRUE);   //set the WebView property to execute JavaScript scripts          mwebview.loadurl ("http://baidu.com");               //loading Web pages that need to be displayed          setcontentview (Mwebview);                             //setting up Web view     }      @Override     / /Set fallback     //override OnKeyDown (Int keycoder,keyevent event) method of activity class      public boolean onkeydown (int keycode, keyevent event)  {         if  ((keycode == keyevent.keycode_back)  &&  mwebview.cangoback ())  {             Mwebview.goback ();  //goback () represents the previous page that returns WebView              return true;        }        return  false;    }}

20 behavior of the androidmanifest.xml file Add permissions to access the network

<?xml version= "1.0"  encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android "    package=" Cn.wuxiaocheng.browser " >      <application        android:allowbackup= " True "        android:icon=" @mipmap/ic_launcher "         android:label= "@string/app_name"          Android:theme= "@style/apptheme"  >        <activity             android:name= ". Mainactivity "            android:label=" @string/ App_name " >            <intent-filter >           &Nbsp;    <action android:name= "Android.intent.action.MAIN"  />                  <category  Android:name= "Android.intent.category.LAUNCHER"  />             </intent-filter>        </activity >    </application>    <uses-permission android:name= "Android.permission.INTERNET"/> </manifest>

The second method:
1. Declare webview in the layout file to display the page first
2, in the activity of the instantiation of WebView
3, call WebView Loadurl () method, set Wevview to display the page
4. To enable WebView to respond to hyperlinks, call the Setwebviewclient () method to set the WebView view
5, in the WebView Web page to open a connection to return to the original page, you can override the Activity class OnKeyDown () method, if you do not do any processing, click on the phone's own return button, the browser will call finish () close the browser, Instead of retreating back to the previous page
6, to access the network also need to add permissions in the Androidmanifest.xml file, or a Web page not available error will occur. < uses-permission android:name="android.permission.INTERNET"/>

The specific code is as follows

Mainactivity.java

package cn.wuxiaocheng.browser;  import android.app.activity; import  android.os.bundle; import android.view.keyevent; import android.webkit.webview;  Import android.webkit.webviewclient;   public class mainactivity extends  Activity {     private WebView mwebview;       @Override     public void oncreate (bundle savedinstancestate)  {          super.oncreate (savedinstancestate);          setcontentview (R.layout.activity_main);          mwebview =  (WebView)  findviewbyid (R.id.id_webview);           mwebview.getsettings (). Setjavascriptenabled (True);       //Setting the WebView property to perform JavasCript script            mwebview.loadurl ("HTTP/ www.baidu.com/");                   //load the page that needs to be displayed          mwebview.setwebviewclient ( new hellowebviewclient  ());        //set Web View      }             @Override      //set fallback      //override OnKeyDown (Int keycoder,keyevent event) method of activity class      public boolean onkeydown (int keycode, keyevent event)  {         if  (Keycode == keyevent.keycode_ back)  && webview.cangoback ())  {              mwebview.goback ();nbsp;//call GoBack () to return to the previous page of WebView               return true;         }          return false;     }             //web View      private class hellowebviewclient  extends WebViewClient {          @Override          public boolean shouldoverrideurlloading (WebView view,  string url)  {              View.loadurl (URL);             return  true;         }     } }

Activity_ Main.xml

<?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/id_webview" android:layout_width= "Fill_parent" Android:lay out_height= "Fill_parent"/></linearlayout>

20 behavior of the androidmanifest.xml file Add permissions to access the network

<?xml version= "1.0"  encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android "    package=" Cn.wuxiaocheng.browser " >      <application        android:allowbackup= " True "        android:icon=" @mipmap/ic_launcher "         android:label= "@string/app_name"          Android:theme= "@style/apptheme"  >        <activity             android:name= ". Mainactivity "            android:label=" @string/ App_name " >            <intent-filter >           &Nbsp;    <action android:name= "Android.intent.action.MAIN"  />                  <category  Android:name= "Android.intent.category.LAUNCHER"  />             </intent-filter>        </activity >    </application>    <uses-permission android:name= "Android.permission.INTERNET"/> </manifest>

For Android? WebView Loading Baidu Homepage

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.