Android webview webpage control access to Internet URL
Add Internet access permission to the androidmanifest. xml file
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com.cloud.android.net" Android: versioncode = "1" Android: versionname = "1.0"> <uses-SDK Android: minsdkversion = "7"/> <! -- Add Internet Access Permissions --> <uses-Permission Android: Name = "android. permission. internet "/> <application Android: icon =" @ drawable/agree_logo "Android: Label =" @ string/app_name "> <activity Android: label = "@ string/app_name" Android: Name = ". netwebviewactivity "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest>
Layout layout file main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/></LinearLayout>
Activity. Java
Package com.cloud.android.net; import android. app. activity; import android. OS. bundle; import android. view. keyevent; import android. webKit. webview; import android. webKit. webviewclient;/*** webview view * @ author Li haiyun * @ email CloudComputing.cc@gmail.com * @ date 2012-02-29 14:42:21 */public class netwebviewactivity extends activity {webview = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); webview = (webview) findviewbyid (R. id. webview); webview. getsettings (). setjavascriptenabled (true); webview. loadurl ("http://www.baidu.com"); webview. setwebviewclient (New hellowebviewclient () ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back & webview. cangoback () {webview. goback (); Return true;} return Super. onkeydown (keycode, event);} private class hellowebviewclient extends webviewclient {@ overridepublic Boolean shouldoverrideurlloading (webview, string URL) {view. loadurl (URL); Return true ;}}}