赞
踩
解决WebView的定位功能、视频全屏播放、下载功能、页面Url的处理、进度条处理
事先说明:
定位功能在安卓6.0需要用户手动确认权限后才能使用
若需在安卓6.0适配WebView的定位功能,则需要在WebView中手动增加用户权限访问
详细可百度安卓6.0权限管理系统,或者采用第三方封装好的权限管理类进行编写(如Bmob)
如果对内容不理解的话,可参考最后的整个类的代码
如果对BaseActivity这个抽象类不理解的话,可以查看下面一篇文章对BaseActivity的介绍
步骤一:webview初始化属性设置:
- /**
- * 初始化网络设置
- */
- private void initWebViewSettings() {
- WebSettings webSettings = wv_web.getSettings();
- //可以有缓存
- webSettings.setAppCacheEnabled(true);
- webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
- //设置支持页面js可用
- webSettings.setJavaScriptEnabled(true);
- webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
- //设置允许访问文件数据
- webSettings.setAllowFileAccess(true);
- //可以使用localStorage
- webSettings.setDomStorageEnabled(true);
- //可以有数据库
- webSettings.setDatabaseEnabled(true);
- //设置定位的数据库路径,若不设置定位数据库路径则无法使用定位功能
- String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
- webSettings.setGeolocationDatabasePath(dir);
- //启用地理定位
- webSettings.setGeolocationEnabled(true);
- }
步骤二:页面Url的处理:(如你的WebView中有个打电话按钮,点击即可调用手机原生打电话)
1、对电话号码的处理;
2、对短信的处理;
3、对邮件的处理;
4、对位置的处理:
5、对地图的处理:
- private class MyWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if (url.startsWith("http:") || url.startsWith("https:")) {
- return false;
- } else if (url.startsWith(WebView.SCHEME_TEL) ||
- url.startsWith("sms:") ||
- url.startsWith(WebView.SCHEME_MAILTO) ||
- url.startsWith(WebView.SCHEME_GEO) ||
- url.startsWith("maps:")) {
- try {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse(url));
- startActivity(intent);
- } catch (android.content.ActivityNotFoundException e) {
- }
- }
- return true;
- }
- }
步骤三:webview视频全屏播放的处理:
- private class MyWebChromeClient extends WebChromeClient {
- @Override
- public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
- if (myView != null) {
- callback.onCustomViewHidden();
- return;
- }
- //设置横屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- myCallBack = callback;
- //隐藏导航栏
- ly_web.removeView(ly_edit);
- //隐藏网页
- ly_web.removeView(wv_web);
- //添加视频
- ly_web.addView(view);
- myView = view;
- }
-
- @Override
- public void onHideCustomView() {
- if (myView == null) {
- return;
- }
- //设置竖屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- //隐藏视频
- ly_web.removeView(myView);
- //添加网页
- ly_web.addView(wv_web);
- //展示导航栏
- ly_web.addView(ly_edit);
- myView = null;
- myCallBack.onCustomViewHidden();
- }
- }
步骤四:进度条进度的处理:
- private class MyWebChromeClient extends WebChromeClient {
-
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- //设置进度条
- if (newProgress <= 40) {
- pb_show.setProgress(newProgress * 2);
- } else if (newProgress >= 80) {
- pb_show.setProgress(newProgress);
- }
- if (newProgress == 100) {
- pb_show.setVisibility(View.GONE);
- } else {
- pb_show.setVisibility(View.VISIBLE);
- }
- }
-
- }
- private class MyWebChromeClient extends WebChromeClient {
- public void onGeolocationPermissionsShowPrompt(String origin,
- GeolocationPermissions.Callback callback) {
- //定位服务
- callback.invoke(origin, true, false);
- super.onGeolocationPermissionsShowPrompt(origin, callback);
- }
- }
- private class MyDownLoadListener implements DownloadListener {
- @Override
- public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
- Uri uri = Uri.parse(url);
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- }
下面是整个类的源码:
- public class WebActivity extends BaseActivity {
-
- //进度条
- private ProgressBar pb_show;
- //内容
- private WebView wv_web;
- private String url;
- //视频切换
- private View myView = null;
- private LinearLayout ly_web = null;
- //内核
- private WebChromeClient chromeClient = null;
- private WebChromeClient.CustomViewCallback myCallBack = null;
- //底部
- private LinearLayout ly_close, ly_go, ly_back, ly_refresh;
- private LinearLayout ly_edit;
-
- @Override
- public void initViews() {
- setContentView(R.layout.activity_web);
- wv_web = (WebView) findViewById(R.id.wv_web);
- ly_web = (LinearLayout) findViewById(R.id.ly_web);
- pb_show = (ProgressBar) findViewById(R.id.pb_show);
- ly_close = (LinearLayout) findViewById(R.id.ly_close);
- ly_go = (LinearLayout) findViewById(R.id.ly_go);
- ly_back = (LinearLayout) findViewById(R.id.ly_back);
- ly_refresh = (LinearLayout) findViewById(R.id.ly_refresh);
- ly_edit = (LinearLayout) findViewById(R.id.ly_edit);
- }
-
- @Override
- public void initListener() {
- ly_close.setOnClickListener(this);
- ly_go.setOnClickListener(this);
- ly_back.setOnClickListener(this);
- ly_refresh.setOnClickListener(this);
- }
-
- @Override
- public void initData() {
- //初始化网络设置
- initWebViewSettings();
- //初始化网路数据
- initWebView();
- }
-
-
- @Override
- public void processClick(View v) {
- switch (v.getId()) {
- case R.id.ly_close:
- finish();
- break;
- case R.id.ly_go:
- if (wv_web.canGoForward()) {
- wv_web.goForward();
- }
- break;
- case R.id.ly_back:
- if (wv_web.canGoBack()) {
- wv_web.goBack();
- }
- break;
- case R.id.ly_refresh:
- wv_web.reload();
- break;
- }
- }
-
- /**
- * 初始化网络设置
- */
- private void initWebViewSettings() {
- WebSettings webSettings = wv_web.getSettings();
- //可以有缓存
- webSettings.setAppCacheEnabled(true);
- webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
- //设置支持页面js可用
- webSettings.setJavaScriptEnabled(true);
- webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
- //设置允许访问文件数据
- webSettings.setAllowFileAccess(true);
- //可以使用localStorage
- webSettings.setDomStorageEnabled(true);
- //可以有数据库
- webSettings.setDatabaseEnabled(true);
- //设置定位的数据库路径
- String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
- webSettings.setGeolocationDatabasePath(dir);
- //启用地理定位
- webSettings.setGeolocationEnabled(true);
- }
-
- /**
- * 初始化网路数据
- */
- private void initWebView() {
- url = getIntent().getStringExtra("url");
- wv_web.loadUrl(url);
- wv_web.setWebViewClient(new MyWebViewClient());
- wv_web.setWebChromeClient(new MyWebChromeClient());
- wv_web.setDownloadListener(new MyDownLoadListener());
- }
-
-
- /**
- * webView渲染类
- */
- private class MyWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if (url.startsWith("http:") || url.startsWith("https:")) {
- return false;
- } else if (url.startsWith(WebView.SCHEME_TEL) ||
- url.startsWith("sms:") ||
- url.startsWith(WebView.SCHEME_MAILTO) ||
- url.startsWith(WebView.SCHEME_GEO) ||
- url.startsWith("maps:")) {
- try {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse(url));
- startActivity(intent);
- } catch (android.content.ActivityNotFoundException e) {
- }
- }
- return true;
- }
- }
-
- /**
- * webView渲染类
- */
- private class MyWebChromeClient extends WebChromeClient {
- @Override
- public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
- if (myView != null) {
- callback.onCustomViewHidden();
- return;
- }
- //设置横屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- myCallBack = callback;
- //隐藏导航栏
- ly_web.removeView(ly_edit);
- //隐藏网页
- ly_web.removeView(wv_web);
- //添加视频
- ly_web.addView(view);
- myView = view;
- }
-
- @Override
- public void onHideCustomView() {
- if (myView == null) {
- return;
- }
- //设置竖屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- //隐藏视频
- ly_web.removeView(myView);
- //添加网页
- ly_web.addView(wv_web);
- //展示导航栏
- ly_web.addView(ly_edit);
- myView = null;
- myCallBack.onCustomViewHidden();
- }
-
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- //设置进度条
- if (newProgress <= 40) {
- pb_show.setProgress(newProgress * 2);
- } else if (newProgress >= 80) {
- pb_show.setProgress(newProgress);
- }
- if (newProgress == 100) {
- pb_show.setVisibility(View.GONE);
- } else {
- pb_show.setVisibility(View.VISIBLE);
- }
- }
-
- public void onGeolocationPermissionsShowPrompt(String origin,
- GeolocationPermissions.Callback callback) {
- //定位服务
- callback.invoke(origin, true, false);
- super.onGeolocationPermissionsShowPrompt(origin, callback);
- }
- }
-
- /**
- * webView下载类
- */
- private class MyDownLoadListener implements DownloadListener {
- @Override
- public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
- Uri uri = Uri.parse(url);
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- }
-
- @Override
- public void onBackPressed() {
- if (myView == null) {
- if (wv_web.canGoBack()) {
- //后退
- wv_web.goBack();
- } else {
- //退出
- finish();
- }
- } else {
- //关闭全屏
- chromeClient.onHideCustomView();
- }
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- wv_web.onResume();
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- wv_web.onPause();
- }
- }
效果图演示:顶部是进度条,底部是4个按钮分别和上面代码中对应,中间则是整个WebView
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。