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); Instantiate the WebView object Mwebview.getsettings (). Setjavascriptenabled (True); Set the WebView property to be able to execute JavaScript script mwebview.loadurl ("http://baidu.com"); Load the page Setcontentview (Mwebview) that needs to be displayed; Set Web View} @Override//Set fallback/overwrite the activity class's onkeydown (int keycoder,keyevent Event) method public Boolean Onkeydow n (int keycode, keyevent event) {if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) { Mwebview.goback (); GoBack () returns true for the previous page returning WebView; } 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> <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);//Set the WebView property to be able to execute JavaScript script mwebview.loadurl ("http://www.b aidu.com/"); Load the Web page mwebview.setwebviewclient (new Hellowebviewclient ()) that needs to be displayed; Set Web View} @Override//Set fallback//overwrite the Activity class's onkeydown (int keycoder,keyevent event) method public Bo Olean onKeyDown (int keycode, keyevent event) {if (keycode = keyevent.keycode_back) && Webview.cangobac K ()) {Mwebview.gobacK (); Call GoBack () to return the previous page of WebView to true; } return false; }//web view Private class Hellowebviewclient extends Webviewclient {@Override public Boole An 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:layout_height= "fill_parent" /> </LinearLayout>
Androidmanifest.xml
<?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> <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