Android:使用控件WebView在程序内打开网页
这里介绍一个使用控件WebView加载网页,并在程序内打开网页,并不调用外部浏览器的方法。
在AndroidManifest.xml设置访问网络权限:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.oicto.apk.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"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest> |
创建布局文件browser.xml,内容如下:
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"></WebView> </LinearLayout> |
java文件,这里修改MainActivity.java文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.WebSettings; import android.webkit.WebChromeClient; public class MainActivity extends Activity { private String url = "http://m.xxxx.com/"; private WebView webView; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browser); init(); } private void init(){ webView = (WebView) findViewById(R.id.webView); webView.loadUrl(url); // 覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebVIew中打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); }else if(url.startsWith("http:") || url.startsWith("https:")) { view.loadUrl(url); } //返回值是true的时候控制网页在WebView中去打开,如果为false调用系统浏览器或第三方浏览器去打开 return true; } //WebViewClient帮助WebView去处理一些页面控制和请求通知 }); //启用支持JavaScript WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); //WebView加载页面优先使用缓存加载 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { //newProgress 1-100之间的整数 if (newProgress == 100) { //网页加载完毕,关闭ProgressDialog closeDialog(); } else { //网页正在加载,打开ProgressDialog openDialog(newProgress); } } private void closeDialog() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); dialog = null; } } private void openDialog(int newProgress) { if (dialog == null) { dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("加载中……"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); } else { dialog.setProgress(newProgress); } } }); } //改写物理按键——返回的逻辑 @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if(keyCode== KeyEvent.KEYCODE_BACK) { //Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show(); if(webView.canGoBack()) { webView.goBack();//返回上一页面 return true; } else { System.exit(0);//退出程序 } } return super.onKeyDown(keyCode, event); } } |
完毕!