Android WebView controls that's enough.

Source: Internet
Author: User

Just done a project to use some of the features of WebView, in the development process encountered a variety of problems, in this record for your reference: (The following code is extracted from the project, for reference)

1. How to add loading progress bar to WebView?

2. How do I set a cookie for WebView?

3. How do I let the server directly invoke the app-side method in WebView?

(add: Because before I do this project, Baidu Google a lot, all described is not very detailed, so I gave it in detail, hope to help everyone)

Mainly is the resolution of these three problems, the following one of our explanations.

1. Add the load progress bar in the WebView.

This is actually very simple, I made a custom progress bar progress, and then use the WebView own Setwebchromeclient () method to update the value of progress in real time. The code is as follows

  

Web.setwebchromeclient (Newwebchromeclient () { Public voidOnprogresschanged (WebView view,intnewprogress) {                //TODO auto-generated Method Stub                Super. onprogresschanged (view, newprogress);
Newprogress is the current progress progress.setprogress (newprogress);
if(newprogress = = 100) {progress.setvisibility (view.gone); } Else{progress.setvisibility (view.visible); } } });

This is WebView's own callback for loading progress, and here I add a method that automatically hides the progress bar when the load is complete.

2. Give WebView Setcookie.

Every person who writes a server is not the same as the naming rules, do not go online to find ways to rigid rigid sets, to be flexible to use. Go directly to the code:

  

cookiesyncmanager.createinstance (Getapplicationcontext ()); Cookiemanager Cookiemanager=cookiemanager.getinstance (); Cookiemanager.setacceptcookie (true); List<Cookie> cookies =Utils.cookieStore.getCookies (); List<String> Listcookie =NewArraylist<string>();  for(inti = 0; I < cookie.size (); i++) {Cookie cookie2=Cookie.get (i); Listcookie.add (Cookie2.getname ()+ "=" + cookie2.getvalue () + ";d omain=" + cookie2.getdomain () + ";p ath=" +Cookie2.getpath ());           Cookiemanager.setcookie (URL, listcookie.get (i)); } cookiesyncmanager.getinstance (). sync (); //force immediate synchronization of Cookies

This is the original sentence in my project, where: Listcookie.add (cookie2.getname () + "=" + cookie2.getvalue () + ";d omain=" + cookie2.getdomain () + ";p ath=" + Cookie2.getpath ());    Why this sentence, and the Internet you can search the wording is different. Before I spent a lot of time here (mainly because the cookie does not understand the principle), in order to let everyone less detours, I explain. The above mentioned that every background developer their development habits are different,

They give you back. The format of the cookie returned may also be different. If you don't understand the definition of a cookie, it's possible to go around bend. First, the cookie is generated by the server side, sent to User-agent, and then next time we need to request the server side to carry up, let the service
To identify whether we are legitimate users. Cookies are saved in Key/value. A pair exists, typically an array format.
The code above is the cookie set that I have to splice on our server to WebView. In addition, sometimes it fails, do not be afraid, add cookiesyncmanager.getinstance () in Onresume (). Startsync (); in OnPause ()
Join Cookiesyncmanager.getinstance (). Stopsync ();
Generally written here, your cookie has been set up successfully.
3. In WebView, let the server can directly invoke the app-side method
This question has nothing to do with the cookie above, and some people think that it is not necessary to set a cookie before it can be called locally to the server. Because they are not related.
First of all, we need to declare some methods, the specific meaning of their own to Baidu is not detailed, the code has comments
//Inject JS methodWebSettings websettings =web.getsettings (); Websettings.setsavepassword (false); Websettings.setsaveformdata (false); Websettings.setsupportzoom (false);        Web.setbackgroundcolor (r.color.transparent); Web.setwebviewclient (Newmywebviewclient ()); //binds a Java object to a JavaScript object, the JavaScript object name is InterfaceName, and the scope is global.Web.addjavascriptinterface (NewJavascriptinterface (datawebactivity. This), "Javascriptinterface");        Web.getsettings (). Setcachemode (Websettings.load_cache_else_network); Web.getsettings (). Setbuiltinzoomcontrols (true); //settings can access filesWeb.getsettings (). Setallowfileaccess (true); //If you have JavaScript on the page you are visiting, WebView must set up JavaScript supportWeb.getsettings (). setjavascriptenabled (true);        Web.getsettings (). Setcachemode (Websettings.load_no_cache); Web.getsettings (). setappcacheenabled (true); Web.getsettings (). setdomstorageenabled (true); Web.getsettings (). setdatabaseenabled (true);

  

Javascriptinterface must implement this interface in its own code, with the same name.
classJavascriptinterface {PrivateContext Mcontext;          PublicJavascriptinterface (Context c) {Mcontext=C; }           //jump to sweat interface@JavascriptInterface Public voidJumpclientgetscore () {finish (); }        //Jump to Redemption history page@JavascriptInterface Public voidJumpclientorder () {            NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                                   }            }, 1);        Finish (); }        //jump to save address interface@JavascriptInterface Public voidjumpclientaddress () {Mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                                   }            }, 100);        Finish (); }        //Share@JavascriptInterface Public voidJumpinvite (FinalString URL,FinalString title,FinalString describe) {mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                }            }, 100); }    }

If you do not understand the original copy of my past, and then the server to follow this interface to invoke, then your problem has been solved. But here's my point:

1. The context must be passed, although this is known, but I would like to focus on it.

2. Must be annotated in front of the method @JavascriptInterface reason Baidu, others have summed up very well. But I know that none of the calls are unsuccessful.

3. Why did I add a delay operation to the method I called? Because this is the project that I did last year, the specific reason also forgot. As if it were a direct call, there might be a large network delay which resulted in the invocation method succeeding, but the method inside

Things do not succeed, mainly for the intent other pages.

Well, science student, do not know the description of the removal of it? Everyone will see, if there is no understanding can be further problems.

  

Android WebView controls that's enough.

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.