Android solves WebView's positioning function, video full-screen playback, download function, page URL processing, progress bar processing

Source: Internet
Author: User

Solve WebView positioning function, video full-screen playback, download function, page URL processing, progress bar processing


Pre-stated:

Positioning function on Android 6.0 requires users to manually confirm permissions before they can use

To manually increase user access in the WebView if you need to webview the location of the Android 6.0 adapter

Details can be Baidu Android 6.0 Rights Management system, or a third-party packaged right management class to write (such as Bmob)


If you do not understand the content, you can refer to the last whole class of code

If you do not understand baseactivity this abstract class, you can view the following article on Baseactivity introduction


Step One: WebView Initialize the property settings:

    /**     * Initialize network settings *     /    private void Initwebviewsettings () {        websettings websettings = wv_web.getsettings ();        //can have cache        websettings.setappcacheenabled (true);        Websettings.setcachemode (Websettings.load_default);        Set support page JS available        websettings.setjavascriptenabled (true);        Websettings.setjavascriptcanopenwindowsautomatically (true);        Set allow access to file data        websettings.setallowfileaccess (true);        You can use Localstorage        websettings.setdomstorageenabled (true);        can have database        websettings.setdatabaseenabled (true);        Set the location of the database path, you cannot use the Locate function String dir = this.getapplicationcontext () If you do not set the location database path        . Getdir ("Database", Context.mode_ PRIVATE). GetPath ();        Websettings.setgeolocationdatabasepath (dir);        Enable Geo        -location websettings.setgeolocationenabled (true);    }
Step two: Page URL processing: (If you have a call button in your WebView, click to call phone native call)

1, the processing of telephone numbers;

2, the processing of SMS;

3, the processing of mail;

4, the processing of the position:

5, the processing of the map:

    Private class Mywebviewclient extends Webviewclient {        @Override public        boolean shouldoverrideurlloading ( WebView view, String URL) {            if (Url.startswith ("http:") | | Url.startswith ("https:")) {                return false;            } else if (Url.startswith (Webview.scheme_tel) | |                    Url.startswith ("SMS:") | |                    Url.startswith (Webview.scheme_mailto) | |                    Url.startswith (Webview.scheme_geo) | |                    Url.startswith ("Maps:")) {                try {                    Intent Intent = new Intent (intent.action_view);                    Intent.setdata (uri.parse (URL));                    StartActivity (intent);                } catch (Android.content.ActivityNotFoundException e) {                }            }            return true;        }    }
Step three: WebView video full-screen playback processing:

    Private class Mywebchromeclient extends Webchromeclient {@Override public void Onshowcustomview (View VI EW, Webchromeclient.customviewcallback callback) {if (MyView! = null) {Callback.oncustomviewhi                Dden ();            Return            }//Set horizontal screen setrequestedorientation (activityinfo.screen_orientation_landscape);            Mycallback = callback;            Hide Navigation bar Ly_web.removeview (Ly_edit);            Hide Page Ly_web.removeview (wv_web);            Add video Ly_web.addview (view);        MyView = view;            } @Override public void Onhidecustomview () {if (MyView = = null) {return;            }//Set vertical screen setrequestedorientation (activityinfo.screen_orientation_portrait);            Hidden video Ly_web.removeview (MyView);            Add Web page Ly_web.addview (wv_web); Display navigation bar Ly_web.addview (ly_edIT);            MyView = null;        Mycallback.oncustomviewhidden (); }    }
Step four: Processing progress bar Progress:

    Private class Mywebchromeclient extends Webchromeclient {        @Override public        void onprogresschanged (WebView view , int newprogress) {            //Set the progress bar            if (newprogress <=) {                pb_show.setprogress (newprogress * 2);            } else if ( Newprogress >=) {                pb_show.setprogress (newprogress);            }            if (newprogress = =) {                pb_show.setvisibility (view.gone);            } else {                pb_show.setvisibility ( view.visible);}}}    

Step five: Handling of positioning functions:

    Private class Mywebchromeclient extends Webchromeclient {public        void Ongeolocationpermissionsshowprompt (String Origin,                                                       Geolocationpermissions.callback Callback) {            //Location Service            Callback.invoke (origin, True, false);            Super.ongeolocationpermissionsshowprompt (origin, callback);        }    }

Step six: Processing of the download function:

    Private class Mydownloadlistener implements Downloadlistener {        @Override public        void Ondownloadstart (String URL, string useragent, String contentdisposition, String mimetype, long contentlength) {            uri uri = uri.parse (URL); 
   intent Intent = new Intent (Intent.action_view, URI);            StartActivity (intent);        }    }

The following is the whole class of source code:

public class Webactivity extends Baseactivity {//progress bar private ProgressBar pb_show;    Content private WebView Wv_web;    Private String URL;    Video Toggle Private View MyView = null;    Private LinearLayout ly_web = null;    Kernel private webchromeclient chromeclient = null;    Private Webchromeclient.customviewcallback mycallback = null;    The bottom private linearlayout ly_close, Ly_go, Ly_back, Ly_refresh;    Private LinearLayout Ly_edit;        @Override public void Initviews () {Setcontentview (r.layout.activity_web);        Wv_web = (WebView) Findviewbyid (R.id.wv_web);        Ly_web = (linearlayout) Findviewbyid (R.id.ly_web);        Pb_show = (ProgressBar) Findviewbyid (r.id.pb_show);        Ly_close = (linearlayout) Findviewbyid (r.id.ly_close);        Ly_go = (linearlayout) Findviewbyid (R.ID.LY_GO);        Ly_back = (linearlayout) Findviewbyid (r.id.ly_back);        Ly_refresh = (linearlayout) Findviewbyid (R.id.ly_refresh); Ly_edit = (linearlayout) Findviewbyid (R.Id.ly_edit);        } @Override public void Initlistener () {Ly_close.setonclicklistener (this);        Ly_go.setonclicklistener (this);        Ly_back.setonclicklistener (this);    Ly_refresh.setonclicklistener (this);        } @Override public void InitData () {//Initialize network settings initwebviewsettings ();    Initialize the network data Initwebview ();                } @Override public void Processclick (View v) {switch (V.getid ()) {r.id.ly_close:                Finish ();            Break                Case R.id.ly_go:if (Wv_web.cangoforward ()) {Wv_web.goforward ();            } break;                Case R.id.ly_back:if (Wv_web.cangoback ()) {wv_web.goback ();            } break;                Case R.id.ly_refresh:wv_web.reload ();        Break }}/** * Initialize network settings */private void initwebviewsettings () {WebseTtings websettings = Wv_web.getsettings ();        can have cache websettings.setappcacheenabled (true);        Websettings.setcachemode (Websettings.load_default);        Set support page JS available websettings.setjavascriptenabled (TRUE);        Websettings.setjavascriptcanopenwindowsautomatically (TRUE);        Set allow access to file data websettings.setallowfileaccess (TRUE);        You can use Localstorage websettings.setdomstorageenabled (true);        can have database websettings.setdatabaseenabled (true);        Sets the database path for positioning String dir = This.getapplicationcontext (). Getdir ("Database", Context.mode_private). GetPath ();        Websettings.setgeolocationdatabasepath (dir);    Enable geo-location websettings.setgeolocationenabled (true);        /** * Initialize network data */private void Initwebview () {URL = getintent (). Getstringextra ("url");        Wv_web.loadurl (URL);        Wv_web.setwebviewclient (New Mywebviewclient ());        Wv_web.setwebchromeclient (New Mywebchromeclient ()); Wv_web. Setdownloadlistener (New Mydownloadlistener ()); }/** * WebView render class */Private class Mywebviewclient extends Webviewclient {@Override public b Oolean shouldoverrideurlloading (WebView view, String URL) {if (Url.startswith ("http:") | |            Url.startswith ("https:")) {return false;                    } else if (Url.startswith (Webview.scheme_tel) | |                    Url.startswith ("SMS:") | |                    Url.startswith (Webview.scheme_mailto) | |                    Url.startswith (Webview.scheme_geo) | |                    Url.startswith ("Maps:")) {try {Intent Intent = new Intent (Intent.action_view);                    Intent.setdata (uri.parse (URL));                StartActivity (Intent);        } catch (Android.content.ActivityNotFoundException e) {}} return true; }}/** * WebView render class */Private class Mywebchromeclient extends WEBCHRomeclient {@Override public void Onshowcustomview (view view, Webchromeclient.customviewcallback callback) {                if (MyView! = null) {Callback.oncustomviewhidden ();            Return            }//Set horizontal screen setrequestedorientation (activityinfo.screen_orientation_landscape);            Mycallback = callback;            Hide Navigation bar Ly_web.removeview (Ly_edit);            Hide Page Ly_web.removeview (wv_web);            Add video Ly_web.addview (view);        MyView = view;            } @Override public void Onhidecustomview () {if (MyView = = null) {return;            }//Set vertical screen setrequestedorientation (activityinfo.screen_orientation_portrait);            Hidden video Ly_web.removeview (MyView);            Add Web page Ly_web.addview (wv_web);            Display navigation bar Ly_web.addview (Ly_edit);            MyView = null; MycallbaCk.oncustomviewhidden (); } @Override public void onprogresschanged (WebView view, int newprogress) {//Set progress bar if (Newprogress <= 40)            {pb_show.setprogress (newprogress * 2);            } else if (newprogress >=) {pb_show.setprogress (newprogress);            } if (newprogress = =) {pb_show.setvisibility (view.gone);            } else {pb_show.setvisibility (view.visible);                                                       }} public void Ongeolocationpermissionsshowprompt (String origin,             Geolocationpermissions.callback Callback) {//Location service Callback.invoke (origin, True, false);        Super.ongeolocationpermissionsshowprompt (origin, callback);        }}/** * WebView Download class */Private class Mydownloadlistener implements Downloadlistener {@Override public void Ondownloadstart (String URL,String useragent, String contentdisposition, String mimetype, long contentlength) {uri uri = uri.parse (URL);            Intent Intent = new Intent (Intent.action_view, URI);        StartActivity (Intent);                }} @Override public void onbackpressed () {if (MyView = = null) {if (Wv_web.cangoback ()) {            Back Wv_web.goback ();            } else {//exit finish ();        }} else {//Close full screen chromeclient.onhidecustomview ();        }} @Override protected void Onresume () {super.onresume ();    Wv_web.onresume ();        } @Override protected void OnPause () {super.onpause ();    Wv_web.onpause (); }}


Demo: The top is the progress bar, the bottom is 4 buttons corresponding to the above code, the middle is the entire webview









Android solves WebView's positioning function, video full-screen playback, download function, page URL processing, progress bar processing

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.