当前位置:   article > 正文

Android解决WebView的定位功能、视频全屏播放、下载功能、页面Url的处理、进度条处理_android x5webview 视频全屏 进度条与底部系统导航栏

android x5webview 视频全屏 进度条与底部系统导航栏

解决WebView的定位功能、视频全屏播放、下载功能、页面Url的处理、进度条处理


事先说明:

定位功能在安卓6.0需要用户手动确认权限后才能使用

若需在安卓6.0适配WebView的定位功能,则需要在WebView中手动增加用户权限访问

详细可百度安卓6.0权限管理系统,或者采用第三方封装好的权限管理类进行编写(如Bmob)


如果对内容不理解的话,可参考最后的整个类的代码

如果对BaseActivity这个抽象类不理解的话,可以查看下面一篇文章对BaseActivity的介绍


步骤一:webview初始化属性设置:

  1. /**
  2. * 初始化网络设置
  3. */
  4. private void initWebViewSettings() {
  5. WebSettings webSettings = wv_web.getSettings();
  6. //可以有缓存
  7. webSettings.setAppCacheEnabled(true);
  8. webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
  9. //设置支持页面js可用
  10. webSettings.setJavaScriptEnabled(true);
  11. webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  12. //设置允许访问文件数据
  13. webSettings.setAllowFileAccess(true);
  14. //可以使用localStorage
  15. webSettings.setDomStorageEnabled(true);
  16. //可以有数据库
  17. webSettings.setDatabaseEnabled(true);
  18. //设置定位的数据库路径,若不设置定位数据库路径则无法使用定位功能
  19. String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
  20. webSettings.setGeolocationDatabasePath(dir);
  21. //启用地理定位
  22. webSettings.setGeolocationEnabled(true);
  23. }
步骤二:页面Url的处理:(如你的WebView中有个打电话按钮,点击即可调用手机原生打电话)

1、对电话号码的处理;

2、对短信的处理;

3、对邮件的处理;

4、对位置的处理:

5、对地图的处理:

  1. private class MyWebViewClient extends WebViewClient {
  2. @Override
  3. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  4. if (url.startsWith("http:") || url.startsWith("https:")) {
  5. return false;
  6. } else if (url.startsWith(WebView.SCHEME_TEL) ||
  7. url.startsWith("sms:") ||
  8. url.startsWith(WebView.SCHEME_MAILTO) ||
  9. url.startsWith(WebView.SCHEME_GEO) ||
  10. url.startsWith("maps:")) {
  11. try {
  12. Intent intent = new Intent(Intent.ACTION_VIEW);
  13. intent.setData(Uri.parse(url));
  14. startActivity(intent);
  15. } catch (android.content.ActivityNotFoundException e) {
  16. }
  17. }
  18. return true;
  19. }
  20. }
步骤三:webview视频全屏播放的处理:

  1. private class MyWebChromeClient extends WebChromeClient {
  2. @Override
  3. public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
  4. if (myView != null) {
  5. callback.onCustomViewHidden();
  6. return;
  7. }
  8. //设置横屏
  9. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  10. myCallBack = callback;
  11. //隐藏导航栏
  12. ly_web.removeView(ly_edit);
  13. //隐藏网页
  14. ly_web.removeView(wv_web);
  15. //添加视频
  16. ly_web.addView(view);
  17. myView = view;
  18. }
  19. @Override
  20. public void onHideCustomView() {
  21. if (myView == null) {
  22. return;
  23. }
  24. //设置竖屏
  25. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  26. //隐藏视频
  27. ly_web.removeView(myView);
  28. //添加网页
  29. ly_web.addView(wv_web);
  30. //展示导航栏
  31. ly_web.addView(ly_edit);
  32. myView = null;
  33. myCallBack.onCustomViewHidden();
  34. }
  35. }
步骤四:进度条进度的处理:

  1. private class MyWebChromeClient extends WebChromeClient {
  2. @Override
  3. public void onProgressChanged(WebView view, int newProgress) {
  4. //设置进度条
  5. if (newProgress <= 40) {
  6. pb_show.setProgress(newProgress * 2);
  7. } else if (newProgress >= 80) {
  8. pb_show.setProgress(newProgress);
  9. }
  10. if (newProgress == 100) {
  11. pb_show.setVisibility(View.GONE);
  12. } else {
  13. pb_show.setVisibility(View.VISIBLE);
  14. }
  15. }
  16. }

步骤五:定位功能的处理:

  1. private class MyWebChromeClient extends WebChromeClient {
  2. public void onGeolocationPermissionsShowPrompt(String origin,
  3. GeolocationPermissions.Callback callback) {
  4. //定位服务
  5. callback.invoke(origin, true, false);
  6. super.onGeolocationPermissionsShowPrompt(origin, callback);
  7. }
  8. }

步骤六:下载功能的处理:

  1. private class MyDownLoadListener implements DownloadListener {
  2. @Override
  3. public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
  4. Uri uri = Uri.parse(url);
  5. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  6. startActivity(intent);
  7. }
  8. }

下面是整个类的源码:

  1. public class WebActivity extends BaseActivity {
  2. //进度条
  3. private ProgressBar pb_show;
  4. //内容
  5. private WebView wv_web;
  6. private String url;
  7. //视频切换
  8. private View myView = null;
  9. private LinearLayout ly_web = null;
  10. //内核
  11. private WebChromeClient chromeClient = null;
  12. private WebChromeClient.CustomViewCallback myCallBack = null;
  13. //底部
  14. private LinearLayout ly_close, ly_go, ly_back, ly_refresh;
  15. private LinearLayout ly_edit;
  16. @Override
  17. public void initViews() {
  18. setContentView(R.layout.activity_web);
  19. wv_web = (WebView) findViewById(R.id.wv_web);
  20. ly_web = (LinearLayout) findViewById(R.id.ly_web);
  21. pb_show = (ProgressBar) findViewById(R.id.pb_show);
  22. ly_close = (LinearLayout) findViewById(R.id.ly_close);
  23. ly_go = (LinearLayout) findViewById(R.id.ly_go);
  24. ly_back = (LinearLayout) findViewById(R.id.ly_back);
  25. ly_refresh = (LinearLayout) findViewById(R.id.ly_refresh);
  26. ly_edit = (LinearLayout) findViewById(R.id.ly_edit);
  27. }
  28. @Override
  29. public void initListener() {
  30. ly_close.setOnClickListener(this);
  31. ly_go.setOnClickListener(this);
  32. ly_back.setOnClickListener(this);
  33. ly_refresh.setOnClickListener(this);
  34. }
  35. @Override
  36. public void initData() {
  37. //初始化网络设置
  38. initWebViewSettings();
  39. //初始化网路数据
  40. initWebView();
  41. }
  42. @Override
  43. public void processClick(View v) {
  44. switch (v.getId()) {
  45. case R.id.ly_close:
  46. finish();
  47. break;
  48. case R.id.ly_go:
  49. if (wv_web.canGoForward()) {
  50. wv_web.goForward();
  51. }
  52. break;
  53. case R.id.ly_back:
  54. if (wv_web.canGoBack()) {
  55. wv_web.goBack();
  56. }
  57. break;
  58. case R.id.ly_refresh:
  59. wv_web.reload();
  60. break;
  61. }
  62. }
  63. /**
  64. * 初始化网络设置
  65. */
  66. private void initWebViewSettings() {
  67. WebSettings webSettings = wv_web.getSettings();
  68. //可以有缓存
  69. webSettings.setAppCacheEnabled(true);
  70. webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
  71. //设置支持页面js可用
  72. webSettings.setJavaScriptEnabled(true);
  73. webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  74. //设置允许访问文件数据
  75. webSettings.setAllowFileAccess(true);
  76. //可以使用localStorage
  77. webSettings.setDomStorageEnabled(true);
  78. //可以有数据库
  79. webSettings.setDatabaseEnabled(true);
  80. //设置定位的数据库路径
  81. String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
  82. webSettings.setGeolocationDatabasePath(dir);
  83. //启用地理定位
  84. webSettings.setGeolocationEnabled(true);
  85. }
  86. /**
  87. * 初始化网路数据
  88. */
  89. private void initWebView() {
  90. url = getIntent().getStringExtra("url");
  91. wv_web.loadUrl(url);
  92. wv_web.setWebViewClient(new MyWebViewClient());
  93. wv_web.setWebChromeClient(new MyWebChromeClient());
  94. wv_web.setDownloadListener(new MyDownLoadListener());
  95. }
  96. /**
  97. * webView渲染类
  98. */
  99. private class MyWebViewClient extends WebViewClient {
  100. @Override
  101. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  102. if (url.startsWith("http:") || url.startsWith("https:")) {
  103. return false;
  104. } else if (url.startsWith(WebView.SCHEME_TEL) ||
  105. url.startsWith("sms:") ||
  106. url.startsWith(WebView.SCHEME_MAILTO) ||
  107. url.startsWith(WebView.SCHEME_GEO) ||
  108. url.startsWith("maps:")) {
  109. try {
  110. Intent intent = new Intent(Intent.ACTION_VIEW);
  111. intent.setData(Uri.parse(url));
  112. startActivity(intent);
  113. } catch (android.content.ActivityNotFoundException e) {
  114. }
  115. }
  116. return true;
  117. }
  118. }
  119. /**
  120. * webView渲染类
  121. */
  122. private class MyWebChromeClient extends WebChromeClient {
  123. @Override
  124. public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
  125. if (myView != null) {
  126. callback.onCustomViewHidden();
  127. return;
  128. }
  129. //设置横屏
  130. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  131. myCallBack = callback;
  132. //隐藏导航栏
  133. ly_web.removeView(ly_edit);
  134. //隐藏网页
  135. ly_web.removeView(wv_web);
  136. //添加视频
  137. ly_web.addView(view);
  138. myView = view;
  139. }
  140. @Override
  141. public void onHideCustomView() {
  142. if (myView == null) {
  143. return;
  144. }
  145. //设置竖屏
  146. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  147. //隐藏视频
  148. ly_web.removeView(myView);
  149. //添加网页
  150. ly_web.addView(wv_web);
  151. //展示导航栏
  152. ly_web.addView(ly_edit);
  153. myView = null;
  154. myCallBack.onCustomViewHidden();
  155. }
  156. @Override
  157. public void onProgressChanged(WebView view, int newProgress) {
  158. //设置进度条
  159. if (newProgress <= 40) {
  160. pb_show.setProgress(newProgress * 2);
  161. } else if (newProgress >= 80) {
  162. pb_show.setProgress(newProgress);
  163. }
  164. if (newProgress == 100) {
  165. pb_show.setVisibility(View.GONE);
  166. } else {
  167. pb_show.setVisibility(View.VISIBLE);
  168. }
  169. }
  170. public void onGeolocationPermissionsShowPrompt(String origin,
  171. GeolocationPermissions.Callback callback) {
  172. //定位服务
  173. callback.invoke(origin, true, false);
  174. super.onGeolocationPermissionsShowPrompt(origin, callback);
  175. }
  176. }
  177. /**
  178. * webView下载类
  179. */
  180. private class MyDownLoadListener implements DownloadListener {
  181. @Override
  182. public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
  183. Uri uri = Uri.parse(url);
  184. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  185. startActivity(intent);
  186. }
  187. }
  188. @Override
  189. public void onBackPressed() {
  190. if (myView == null) {
  191. if (wv_web.canGoBack()) {
  192. //后退
  193. wv_web.goBack();
  194. } else {
  195. //退出
  196. finish();
  197. }
  198. } else {
  199. //关闭全屏
  200. chromeClient.onHideCustomView();
  201. }
  202. }
  203. @Override
  204. protected void onResume() {
  205. super.onResume();
  206. wv_web.onResume();
  207. }
  208. @Override
  209. protected void onPause() {
  210. super.onPause();
  211. wv_web.onPause();
  212. }
  213. }


效果图演示:顶部是进度条,底部是4个按钮分别和上面代码中对应,中间则是整个WebView









声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/308948
推荐阅读
相关标签
  

闽ICP备14008679号