关于Android webView的用法
2014-07-07 15:25
471 查看
如果你加载的网页需要Flash 插件支持,则可以先检测是否安装了Flash插件,如果没有安装该插件则下载该插件
private boolean checkFlash() {
PackageManager pm = getPackageManager();
List<PackageInfo> infoList = pm
.getInstalledPackages(PackageManager.GET_SERVICES);
for (PackageInfo info : infoList) {
if ("com.adobe.flashplayer".equals(info.packageName)) {
return true;
}
}
AlertDialog.Builder builder = new Builder(this);
// builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(getString(R.string.flash_is_need_to_display_the_map));
// builder.setMessage(result.getUpdateDate());
builder.setCancelable(false);
builder.setNegativeButton(getString(R.string.download), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
installFlashApk();
}
private void installFlashApk() {
Intent installIntent = new Intent(
"android.intent.action.VIEW");
installIntent.setData(Uri
.parse("market://details?id=com.adobe.flashplayer"));
startActivity(installIntent);
finish();
}
});
builder.setPositiveButton(R.string.btn_cancle, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
return false;
}
//webview的一些设置。
mWebView=(WebView) findViewById(R.id.view1);
mWebView.getSettings().setDefaultTextEncodingName("gbk") ;
mWebView.getSettings().setJavaScriptEnabled(true);// 设置支持javascript
mWebView.requestFocus();// 获取触摸焦点
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);// 取消滚动条
mWebView.getSettings().setBuiltInZoomControls(true); // 构建缩放控制
mWebView.getSettings().setSupportZoom(true); // 设置支持缩放
mWebView.getSettings().setBlockNetworkImage(false);
mWebView.getSettings().setBlockNetworkLoads(false);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); // 在当前的webview中跳转到新的url
if (url.startsWith("mailto:") || url.startsWith("geo:") ||url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
rememberNextUrl(url);
return true;
}
private void rememberNextUrl(String url) {
if(!url.startsWith("tel:")){
nextUrl=url;
}
LogUtil.i("url=="+url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//progressBar1.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
rememberNextUrl(url);
// TODO Auto-generated method stub
try {
new Handler().postDelayed(new Runnable() {
public void run() {
imageView.setVisibility(View.GONE);
progressBar1.setVisibility(View.GONE);
}
}, 1000);
} catch (Exception ex) {
ex.printStackTrace();
}
super.onPageFinished(view, url);
rememberNextUrl(url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if(failingUrl.startsWith("tel:")){
view.loadUrl(nextUrl);
// showShortToastMessage("拨打电话");
Intent intent = new Intent();
// intent.setAction(Intent.ACTION_CALL);
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse(failingUrl));
startActivity(intent);
}else{
view.loadUrl(nextUrl);
showShortToastMessage("亲,您的手机可能不支持该功能哦");
// super.onReceivedError(view, errorCode, description, failingUrl);
}
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
//handler.cancel(); 默认的处理方式,WebView变成空白页
handler.proceed();//接受证书
//handleMessage(Message msg); 其他处理
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
return super.shouldInterceptRequest(view, url);
}
});
//第一个参数为调用的java对象 第二个参数 js里的 java对象名,js可以通过该对象名调用java对象的方法
mWebView.addJavascriptInterface(this, "javaObj");
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.loadUrl(url);
/***
* 自定义WebChromeClient,做选择图片处理
* @author spring sky
* 创建时间:Aug 19, 20133:40:46 PM
*/
private class MyWebChromeClient extends WebChromeClient {
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
if (mUploadMessage != null) return;
mUploadMessage = uploadMsg;
//selectImage();
createFileIntent();
}
// For Android < 3.0
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
createFileIntent();
}
// For Android > 4.1.1
// public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
// openFileChooser(uploadMsg, acceptType);
// }
// @Override
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
// createFileIntent(acceptType,capture);
createFileIntent();
// createDefaultOpenableIntent();
// createChooserIntent();
}
private void createFileIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
}
private void createFileIntent(String acceptType, String capture) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(acceptType);
startActivityForResult(
Intent.createChooser(intent,capture),
FILECHOOSER_RESULTCODE);
}
private Intent createDefaultOpenableIntent() {
// Create and return a chooser with the default OPENABLE
// actions including the camera, camcorder and sound
// recorder where available.
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(),
createSoundRecorderIntent());
chooser.putExtra(Intent.EXTRA_INTENT, i);
startActivityForResult(
Intent.createChooser(i, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
return chooser;
}
private Intent createChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");
startActivityForResult(
Intent.createChooser(chooser, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
return chooser;
}
private Intent createCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + "browser-photos");
cameraDataDir.mkdirs();
mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
return cameraIntent;
}
private Intent createCamcorderIntent() {
return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
}
private Intent createSoundRecorderIntent() {
return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
if(result!=null){
LogUtil.i("图片地址=="+result.getPath());
}
mUploadMessage = null;
}
}
如果你加载的网页需要Flash 插件支持,则可以先检测是否安装了Flash插件,如果没有安装该插件则下载该插件
private boolean checkFlash() {
PackageManager pm = getPackageManager();
List<PackageInfo> infoList = pm
.getInstalledPackages(PackageManager.GET_SERVICES);
for (PackageInfo info : infoList) {
if ("com.adobe.flashplayer".equals(info.packageName)) {
return true;
}
}
AlertDialog.Builder builder = new Builder(this);
// builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(getString(R.string.flash_is_need_to_display_the_map));
// builder.setMessage(result.getUpdateDate());
builder.setCancelable(false);
builder.setNegativeButton(getString(R.string.download), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
installFlashApk();
}
private void installFlashApk() {
Intent installIntent = new Intent(
"android.intent.action.VIEW");
installIntent.setData(Uri
.parse("market://details?id=com.adobe.flashplayer"));
startActivity(installIntent);
finish();
}
});
builder.setPositiveButton(R.string.btn_cancle, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
return false;
}
//webview的一些设置。
mWebView=(WebView) findViewById(R.id.view1);
mWebView.getSettings().setDefaultTextEncodingName("gbk") ;
mWebView.getSettings().setJavaScriptEnabled(true);// 设置支持javascript
mWebView.requestFocus();// 获取触摸焦点
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);// 取消滚动条
mWebView.getSettings().setBuiltInZoomControls(true); // 构建缩放控制
mWebView.getSettings().setSupportZoom(true); // 设置支持缩放
mWebView.getSettings().setBlockNetworkImage(false);
mWebView.getSettings().setBlockNetworkLoads(false);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); // 在当前的webview中跳转到新的url
if (url.startsWith("mailto:") || url.startsWith("geo:") ||url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
rememberNextUrl(url);
return true;
}
private void rememberNextUrl(String url) {
if(!url.startsWith("tel:")){
nextUrl=url;
}
LogUtil.i("url=="+url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//progressBar1.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
rememberNextUrl(url);
// TODO Auto-generated method stub
try {
new Handler().postDelayed(new Runnable() {
public void run() {
imageView.setVisibility(View.GONE);
progressBar1.setVisibility(View.GONE);
}
}, 1000);
} catch (Exception ex) {
ex.printStackTrace();
}
super.onPageFinished(view, url);
rememberNextUrl(url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if(failingUrl.startsWith("tel:")){
view.loadUrl(nextUrl);
// showShortToastMessage("拨打电话");
Intent intent = new Intent();
// intent.setAction(Intent.ACTION_CALL);
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse(failingUrl));
startActivity(intent);
}else{
view.loadUrl(nextUrl);
showShortToastMessage("亲,您的手机可能不支持该功能哦");
// super.onReceivedError(view, errorCode, description, failingUrl);
}
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
//handler.cancel(); 默认的处理方式,WebView变成空白页
handler.proceed();//接受证书
//handleMessage(Message msg); 其他处理
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
return super.shouldInterceptRequest(view, url);
}
});
//第一个参数为调用的java对象 第二个参数 js里的 java对象名,js可以通过该对象名调用java对象的方法
mWebView.addJavascriptInterface(this, "javaObj");
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.loadUrl(url);
/***
* 自定义WebChromeClient,做选择图片处理
* @author spring sky
* 创建时间:Aug 19, 20133:40:46 PM
*/
private class MyWebChromeClient extends WebChromeClient {
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
if (mUploadMessage != null) return;
mUploadMessage = uploadMsg;
//selectImage();
createFileIntent();
}
// For Android < 3.0
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
createFileIntent();
}
// For Android > 4.1.1
// public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
// openFileChooser(uploadMsg, acceptType);
// }
// @Override
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
// createFileIntent(acceptType,capture);
createFileIntent();
// createDefaultOpenableIntent();
// createChooserIntent();
}
private void createFileIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
}
private void createFileIntent(String acceptType, String capture) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(acceptType);
startActivityForResult(
Intent.createChooser(intent,capture),
FILECHOOSER_RESULTCODE);
}
private Intent createDefaultOpenableIntent() {
// Create and return a chooser with the default OPENABLE
// actions including the camera, camcorder and sound
// recorder where available.
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(),
createSoundRecorderIntent());
chooser.putExtra(Intent.EXTRA_INTENT, i);
startActivityForResult(
Intent.createChooser(i, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
return chooser;
}
private Intent createChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");
startActivityForResult(
Intent.createChooser(chooser, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
return chooser;
}
private Intent createCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + "browser-photos");
cameraDataDir.mkdirs();
mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
return cameraIntent;
}
private Intent createCamcorderIntent() {
return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
}
private Intent createSoundRecorderIntent() {
return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
if(result!=null){
LogUtil.i("图片地址=="+result.getPath());
}
mUploadMessage = null;
}
}
相关文章推荐
- 关于android的WebView
- android 4.0中关于webview加载flash并使flash全屏的问题(附demo)
- [Android实例] 关于webview如何自动登录保存登录信息
- 关于android WebViewClient的方法解释
- android程序中关于webview加载html文件
- 关于Android WebView对JS与href支持
- Android中关于WebView的使用办法
- 关于android WebViewClient的方法解释
- Android_WebView用法
- android WebView, WebChromeClient和WebViewClient加载网页基本用法
- android webview用法小结1
- [Android实例] 关于webview如何自动登录保存登录信息
- android webview用法小结1
- 关于android WebViewClient的方法解释
- android webView用法
- android webview用法小结2 java与javascript的交互
- 关于android WebViewClient和WebChromeClient
- Android中的WebView控件用法(二)
- [Android] 关于WebView的内存泄露问题
- 关于android webview读取js全局变量或者函数返回值