Use of WebView

Source: Internet
Author: User

1. Set the URL address of the Web page WebView loaded
Webview.loadurl (URL address);
2. Set various events for WebView 2.1 webview.setwebviewclient
Webview.setwebviewclient (New Webviewclient ()); Relatively simple processing requests: such as loading Web page javascript Dialogs (JS Script dialog box), Favicons (add favorite logo), titles (title), and Progress (progress bar, page pre-loading, post-load events, etc.

Overridden Method 1 (called when WebView accesses other URL addresses): View is the current webview; URL for the current webview to jump to the new WebView URL address;
@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true;}

Overridden Method 2 (called when the current Web page starts to load): Favicon is the icon for the current WebView
@Overridepublic void onpagestarted (WebView view, String URL, Bitmap favicon) {//Here Initialize and display progressdielogif (Progdlg = = NULL || !progdlg.isshowing () &&refreshview==null) {              Progdlg = new ProgressDialog (GetContext ());              Progdlg.setmessage ("Loading, please wait ... ");              Progdlg.setcanceledontouchoutside (false);        WebView do not load the picture if (Build.VERSION.SDK_INT >=) {mwvfragmentindex.getsettings () before initializing. Setloadsimagesautomatically (TRUE);} else {mwvfragmentindex.getsettings (). setloadsimagesautomatically (False);}}}

Overridden Method 3 (called when the current page load is complete):
Page load complete @overridepublic void onpagefinished (WebView view, String URL) {         //progressdielog disappears Progdlg.dismiss ();// After the framework loads, start loading the picture if (!mwvfragmentuser.getsettings (). getloadsimagesautomatically ()) {mwvfragmentuser.getsettings (). Setloadsimagesautomatically (True);}}


2.2 Webview.setwebchromeclient

Webview.setwebchromeclient (new);//When making pages that are compatible with JavaScript on the internet and when invoking JavaScript dialogs, or more complex inline operations, Recommended use of Webchromeclient

Gets the title of the current page webview.setwebchromeclient (new Webchromeclient () {@Overridepublic void Onreceivedtitle (WebView view, String title) {Webviewtitlelist.add (title);//Add the current page title to the ListView Ac.webviewtitlecallback (title);// Callback the page title to Mainactivity}});

3. Handling WebView and Activity return button conflicting ideas: 1, overriding onkeydown Method 2 in fragment, overriding OnKeyDown method in Mainactivity, and invoking fragment method of the corresponding onkeydown 3. But there is no solution to the main page. Double-click return conflict with WebView
Key code: in Fragment:
public boolean onKeyDown1 (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {if (Webview.cangoback ()) {/ /If the WebView can also return to the previous page webviewback ();//Call the Webviewback method to return to the previous page} else {webviewtitlelist.clear ();//If you cannot return, If the description has reached the first page of WebView, then empty the Listviewreturn false to save the page title; Webviewback ();} return true;}

Webviewback () Method:
public void Webviewback () {if (Mwvfragmentall.cangoback ()) {Webviewtitlelist.remove (Webviewtitlelist.size ()-1);// Deletes the current title of the ListView Data Ac.webviewtitlecallback (Webviewtitlelist.get (Webviewtitlelist.size ()-1));// Callback the title of the previous page to the homepage mwvfragmentall.goback ();//Return to previous page}}



In mainactivity:
@Overridepublic boolean onKeyDown (int keycode, keyevent event) {(indexfragment) indexfragment). OnKeyDown1 (KeyCode, event);//Call fragment in Onkeydown<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>return false;}

4. Get cookie data in WebView
Cookiemanager Cookiemanager = Cookiemanager.getinstance (); String cookiestr = Cookiemanager.getcookie (Httpmodle.http_url_user);//Get Cookie data string[] Cookiearr = Cookiestr.split (";");/ /Based on: Split cookie string
Example: Cookie Data
Phpsessidphpsessid=s9ts5t4pa0n6arieiscnkavtd6;
URL-encoded shopping cart list, decoded as JSON string cartlist=%7b%2215%22%3a%7b%22num%22%3w3%2c%22shenyu%22%3a%223582%22%2c%22money%22%3a% 221.00%22%7d%2c%2213%22%3a%7b%2;
cartlistoutputstring = Urldecoder.decode (cartlist, "UTF-8");//urldecod transcoding to Utf-8

MD5 user name uid=d66dbvecgvijvagjvlnyvggtvfftdgugvvjquwvq after encryption;
MD5 Password after encryption
Ushell=d66dbvegavijvaajvgecgfndvfebugibuqftvlrzbaagavvwggcdvgfquq4fvgucxvcdwvmcgalqbfyfgw

Other if used in the late add ...



Use of WebView

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.