最近项目中用到安卓原生APP 嵌套H5网页,使用安卓原生WebView实现,主要遇到以下两种问题,特此记录:
- 在H5页面,返回直接回到APP中
- 监听安卓的返回事件就行了
-
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK&&shejian.canGoBack()){ shejian.goBack(); return true; }else{ onBackPressed(); } return super.onKeyDown(keyCode, event); }
-
- 监听安卓的返回事件就行了
- 在嵌套的网页中,点击链接跳转至另一款APP中报错
- 好像是因为WebView 只会以Http、Https的形式加载链接,导致 liktin://类链接无法加载
- 处理方法:重写webView的 WebViewClient 方法,判断url是否是此类链接,具体代码如下:
WebViewClient webViewClient = new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView wv, String url) { if (url == null) return false; try { //判断是不是打开APP的链接 if (url.startsWith("liktin://")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash) Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.shejian360.com/download/zxks/androidDownload.html")); startActivity(intent); return true;//没有安装该app时,返回true,表示拦截自定义链接,跳转到下载页面 } //处理http和https开头的url wv.loadUrl(url); return true; } }; //设置WebView 的WebViewClient shejian.setWebViewClient(webViewClient);
- 嵌套的网页中,无法调起图片上传
- 试了网上说的几种方法,最后用此方法解决问题
-
private ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> uploadMessage; public static final int REQUEST_SELECT_FILE = 100; private final static int FILECHOOSER_RESULTCODE = 2;
shejian.setWebChromeClient(new WebChromeClient() { // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); shejian.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { openFileChooser(uploadMsg); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooser(uploadMsg); } // For Lollipop 5.0+ Devices @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (uploadMessage != null) { uploadMessage.onReceiveValue(null); uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { uploadMessage = null; return false; } return true; } });
-
- 试了网上说的几种方法,最后用此方法解决问题
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); uploadMessage = null; } } else if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } }