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:


The Java code is as follows:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 /** * 通用的webview,只需要传入2个参数即可, param1:title 标题 param2:url 网站 *   *  */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);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);// 加载需要显示的网页webview.addJavascriptInterface(new JavascriptInterface(PublicWebView.this), Phoenix_Android);webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 设置WebView属性,能够执行Javascript脚本webview.getSettings().setJavaScriptEnabled(true);// 加载需要显示的网页webview.loadUrl(url);// 设置Web视图webview.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;public JavascriptInterface(Context context) {this.context = context;}public void back(String module, int status, String place) {reFreshBlance();if (status == -1) { // 用户取消了return;} else if (status == 0) {Toast.makeText(PublicWebView.this, 充值请求失败,请稍后重试..., Toast.LENGTH_SHORT).show();return;} else if (status == 1) {// 充值请求成功reFreshBlance();// 1.用户中心进来的有两个参数// a.bet参数是返回购彩界面// b.userCenter返回用户中心 }}}// 监听 所有点击的链接,如果拦截到我们需要的,就跳转到相对应的页面。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);}} // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {// webview.goBack();// 返回前一个页面finish();return true;}return super.onKeyDown(keyCode, event);}}

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

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