WebView Pits--small function article

Source: Internet
Author: User

These two days were responsible for modifying several bugs that opened the company's mobile station in WebView. itself is not difficult to solve, online search has, but it is necessary to record down as a memo.

Uploading files in WebView

The effect here is similar to the PC-side upload file effect, click to open a file selector, upload file pictures and so on.
openFileChooser()Methods are overloaded because the method parameters that are called in different systems are not the same, depending on the comment.
ValueCallback<Uri> mUploadMessageThe purpose of this as a member variable is to be called when the System file selector is open finish() onActivityResult() .
The specific implementation code is as follows:

Private void Initwebview() {webview.setwebchromeclient (NewMywebchromeclient ());}PrivateValuecallback<uri> Muploadmessage;Private  class mywebchromeclient extends webchromeclient {    //JS upload file <input type= "file"/> Event capture    //Android > 4.1.1 Call this method     Public void Openfilechooser(valuecallback<uri> uploadmsg, String accepttype, string capture)      {muploadmessage = uploadmsg; Intent Intent =NewIntent (intent.action_get_content);      Intent.addcategory (intent.category_openable); Intent.settype (Textutils.isempty (accepttype)?"image/*": Accepttype); Mobilesiteactivity. This. Startactivityforresult (Intent.createchooser (Intent, toolbox.getstring (R.string.web_activity_please_chooser)),    Mobilesiteactivity.filechooser_resultcode); }//3.0 + Call this method     Public void Openfilechooser(valuecallback<uri> uploadmsg, String accepttype) {Openfilechooser (uploadmsg, Accepttype,NULL); }//Android < 3.0 call this method     Public void Openfilechooser(valuecallback<uri> uploadmsg) {Openfilechooser (uploadmsg,NULL); }  }@Override  protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {if(Filechooser_resultcode = = Requestcode) {if(NULL= = Muploadmessage)return; Uri result = Data = =NULL|| ResultCode! = RESULT_OK?NULL: Data.getdata ();      Muploadmessage.onreceivevalue (result); Muploadmessage =NULL;return; }  }
WebView the map in the page is white and does not show the map

The problem is that you didn't turn on JavaScript support. Just open it.

webView.setJavaScriptEnabled(true);// 设置支持javascript脚本
WebView in jump system dialing keyboard

Extrapolate, since to open the system Dial keyboard, the mailbox, map can also support.

webView.setWebViewClient(new WebViewClient() {      publicbooleanshouldOverrideUrlLoading(WebView view, String url) {        if (url.startsWith("mailto:") || url.startsWith("geo:") || url.startsWith("tel:")) {          new Intent(Intent.ACTION_VIEW, Uri.parse(url));          startActivity(intent);          returntrue;        }        view.loadUrl(url);        returntrue;      }    });
Support for download in WebView

Here to WebView set a download monitoring, listening will callback to you, here can open the System browser to activate the download, with Downloadmanger directly to download can also, I chose the first way, this way I think more in line with the user's habit ha.

 webView.setDownloadListener(new MyWebViewDownLoadListener());
privateclass MyWebViewDownLoadListener implements DownloadListener {    @Override    publicvoidonDownloadStart(String url, String userAgent, String contentDisposition,        long contentLength) {      if (null != url) {        Uri uri = Uri.parse(url);        new Intent(Intent.ACTION_VIEW, uri);        startActivity(intent);      }    }  }

WebView Pits--small function article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.