Android中WebView的定位功能、视频全屏播放、下载功能、页面Url的处理、进度条处理
程序员文章站
2022-04-26 18:29:28
...
一、WebView 初始化设置
/**
* 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);
}
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;
}
}
三、视频全屏播放
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();
}
}
示例图
下一篇: dfs 剪枝