Android embedded web page webview click the link to jump to the Activity in our application

Source: Internet
Author: User

Android embedded web page webview click the link to jump to the Activity in our application

In a large Android project, because the client is too late to be updated and implemented, some web pages are often embedded (in some large Internet companies, PC products always run in front of the client ), for example, an activity page can usually be embedded with HTML 5 to adapt to mobile phones. However, these webpages have many links, but some of these links are implemented by our client. For example, if there is a registration link, the client also implements the registration function, instead of continuing to jump to the webpage registration page, we open a client to register an Activity. You can achieve this by using the following methods:

 

Xml file, as follows:

 


The java code is as follows:

 

 

/*** For general webview, you only need to input two parameters. param1: 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. onCreate (savedInstanceState); setContentVie W (R. layout. public_webview); Intent intent = getIntent (); String url = intent. getStringExtra (url); String title = intent. getStringExtra (title); showTitle = intent. getBooleanExtra (show_title, true); public_webview_top = (LinearLayout) findViewById (R. id. public_webview_top); if (showTitle) {public_webview_top.setVisibility (View. VISIBLE);} else {public_webview_top.setVisibility (View. GONE);} TextView titletv = (TextView) findViewById (R. id. public_webview_title); titletv. setText (title); progressBar = (ProgressBar) findViewById (R. id. public_webview_progressbar); webview = (WebView) findViewById (R. id. public_webview_webview); // load the webview of the webpage to be displayed. addJavascriptInterface (new JavascriptInterface (PublicWebView. this), Phoenix_Android); webview. setScrollBarStyle (View. SCROLLBARS_INSIDE_OVERLAY); // sets the WebView attribute to execute Javascr. The error message returned when the error message returned. getSettings (). setJavaScriptEnabled (true); // load the webview of the page to be displayed. loadUrl (url); // sets the webview of the Web View. setWebViewClient (new MyWebViewClient (); webview. setWebChromeClient (new WebChromeClient () {@ Overridepublic void onProgressChanged (WebView view, int newProgress) {if (newProgress = 100) {progressBar. setVisibility (View. GONE);} else {progressBar. setVisibility (View. VISIBLE);} super. onProgressChanged (View, newProgress) ;}}); back = (ImageButton) findViewById (R. id. public_webview_back); back. setOnClickListener (new BackListener (PublicWebView. this, null, 1);} @ SuppressLint (NewApi) public class JavascriptInterface {@ SuppressWarnings (unused) private Context context Context; public JavascriptInterface (context Context) {this. context = context;} public void back (String module, int status, String place) {reFreshBlance (); If (status =-1) {// The user canceled return;} else if (status = 0) {Toast. makeText (PublicWebView. this, recharge request failed. Please try again later ..., toast. LENGTH_SHORT ). show (); return;} else if (status = 1) {// reFreshBlance () Successful recharge request; // 1. the user center has two parameters //. the bet parameter is returned to the purchase page // B. userCenter returns user center }}// listens for all the clicked links. If we intercept what we need, we will 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/phoneRegiste. do) {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) ;}// override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & webview. canGoBack () {// webview. goBack (); // return the previous page finish (); return true;} return super. onKeyDown (keyCode, event );}}



 

 

 

 

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.