Android Embedded page WebView Click on the link to jump to activity in our app

Source: Internet
Author: User

In a large Android project, because the client is too late to update and implement, often embedded some Web pages (in some large internet companies, PC products always run in front of the client), such as the active page, usually can be embedded in the HTML5 implementation of the page, can be adapted to the mobile phone. But these pages have a lot of links, but some of these links have been implemented by our client, such as a registered link, in fact, the client has also implemented the registration function, we do not want to continue to jump to the page registration, but open the client a registered activity, can be implemented in the following ways:

XML file, as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= " Android "android:id=" @+id/rva_main "android:layout_width=" match_parent "android:layout_height=" Match_parent "a Ndroid:background= "#FFFFFF" android:orientation= "vertical" > <webview android:id= "@+id/public_webview_w Ebview "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:background=" #a Bcabc "/> <progressbar android:id=" @+id/public_webview_progressbar "style="? Android:attr/progre Ssbarstylesmall "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layo Ut_alignparentleft= "true" android:layout_centervertical= "true" android:visibility= "visible"/> <lin Earlayout android:id= "@+id/public_webview_top" android:layout_width= "Fill_parent" Android:layout_heig ht= "52dip" Android:layout_Alignparenttop= "true" android:background= "@drawable/top_bj" > <relativelayout android:l Ayout_width= "Fill_parent" android:layout_height= "52dip" android:layout_gravity= "center" android:backg round= "@drawable/top_bj" > <imagebutton android:id= "@+id/public_webview_back" style= "@st             Yle/back_button_style "/> <imageview android:layout_torightof=" @+id/public_webview_back " style= "@style/back_line_style"/> <textview android:id= "@+id/public_webview_title" a                    ndroid:layout_torightof= "@+id/public_webview_back" style= "@style/top_title_style"/> </RelativeLayout> </LinearLayout> </RelativeLayout>

The Java code is as follows:

/** * General WebView, only need to pass in 2 parameters, param1:title title Param2:url website * * */public class Publicwebview extends Activity {private WebView webview;private ImageButton back;private linearlayout public_webview_top;private boolean showtitle;private ProgressBar ProgressBar; @SuppressLint ("Javascriptinterface") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.public_webview); Intent Intent = Getintent (); String url = intent.getstringextra ("url"); String title = Intent.getstringextra ("title"); Showtitle = Intent.getbooleanextra ("Show_title", true);p Ublic_webview_ top = (linearlayout) Findviewbyid (, if (showtitle) {public_webview_top.setvisibility ( view.visible);} else {public_webview_top.setvisibility (view.gone);} TextView Titletv = (TextView) Findviewbyid (; Titletv.settext (title);p Rogressbar = ( ProgressBar) Findviewbyid (; webview = (WebView) Findviewbyid (R.ID.PUblic_webview_webview);//Load the page webview.addjavascriptinterface (new Javascriptinterface (publicwebview.this) that needs to be displayed, " Phoenix_android "); Webview.setscrollbarstyle (view.scrollbars_inside_overlay);//Set WebView property, Ability to execute JavaScript script webview.getsettings (). Setjavascriptenabled (true);//Load the page webview.loadurl (URL) that needs to be displayed;// Set Web View webview.setwebviewclient (new Mywebviewclient ()); Webview.setwebchromeclient (new Webchromeclient () {@ overridepublic void onprogresschanged (WebView view, int newprogress) {if (newprogress = =) {progressbar.setvisibility (View.gone);} Else{progressbar.setvisibility (view.visible);} Super.onprogresschanged (view, newprogress);}); back = (ImageButton) Findviewbyid (; Back.setonclicklistener (New Backlistener ( Publicwebview.this, NULL, 1));} @SuppressLint ("Newapi") public class Javascriptinterface {@SuppressWarnings ("unused") Private Context Context;public Javascriptinterface (Context context) {This.context = context;} public void Back (string module, int status, string plaCE) {refreshblance (), if (status = =-1) {//user canceled return;} else if (status = = 0) {toast.maketext (publicwebview.this, "Reload request lost Please try again later ... ", Toast.length_short). Show (); return;} else if (status = = 1) {//top-up request succeeded Refreshblance ();//1. The User center comes in with two parameters// parameter is back to the purchase interface//B.usercenter back to the user Center}}}//listen to all clicked links, such as If you intercept what we need, jump to the corresponding page. Private class Mywebviewclient extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {System.out.println ("----------------------shouldoverrideurlloading: URL: "+ url"), if (URL! = null && url.contains ("/m/")) {Intent Intent = new Intent (publicwebview.this , Registerbyphone.class); PublicWebView.this.startActivity (Intent);  Finish (); return true;} Return super.shouldoverrideurlloading (view, URL);} @Overridepublic void onpagefinished (WebView view, String URL) {view.getsettings (). Setjavascriptenabled (True); super.onpagefinished (view, URL);}} OnKeyDown (int keycoder,keyevent event) method that overrides the activity class public Boolean onKeyDown (iNT KeyCode, KeyEvent event) {if (keycode = = Keyevent.keycode_back && webview.cangoback ()) {//Webview.goback ();// Returns the previous page, finish (); return true;} Return Super.onkeydown (KeyCode, event);}}

Android Embedded page WebView Click on the link to jump to activity in our app

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: 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.