Android realizes WebView click intercept jump native

  • 2021-11-30 01:24:36
  • OfStack

1. Set the Web view first

webview.setWebViewClient(new MyWebViewClient());
webview1.setWebViewClient(new MyWebViewClient());

2. Block the clicked link and jump to the corresponding page


 //  Eavesdropping   All clicked links, if we intercept what we need, jump to the corresponding page. 

 private class MyWebViewClient extends WebViewClient {

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   Log.e("tsg","url==="+url);
 // Proceed here url Intercept 
   if (url != null && url.contains(https://www.ofstack.com)) {
  Required for string interception 
    String str="";
    str=url.substring(url.lastIndexOf("id/"),url.length());
    str=str.substring(3,str.indexOf("."));
    Log.e("tsg","id==="+str);
//    Toast.makeText(mActivity, "njhnuj", Toast.LENGTH_SHORT).show();
 // Jump to the page that needs to be jumped 
    Intent intent = new Intent();
    intent = new Intent(getActivity(), SPProductDetailActivity.class);
    intent.putExtra("goodsID",str);
    getActivity().startActivity(intent);
    return true;

   }
   return super.shouldOverrideUrlLoading(view, url);
  }
  @Override

  public void onPageFinished(WebView view, String url) {
   view.getSettings().setJavaScriptEnabled(true);
   super.onPageFinished(view, url);
  }

 }

Additional knowledge: Android9.0 WebView blank page

Recently, I met some mobile phone 9.0 systems of online customers, and the pages using webview were blank, so the pages could not be loaded normally. . .

The reason is:

There is one new feature in Android version 9.0 as follows

All applications use HTTPS

This is an affirmation of Android secure users. All applications on AndroidPie use HTTPS by default. Android cares about your privacy.

That is, starting with Android 9.0 (API level 28), plaintext support is disabled by default. Therefore, url of http cannot be loaded in webview.

Solution:

Add android to the APplication tag in the AndroidManifest. xml file: usesCleartextTraffic = "true".

android:usesCleartextTraffic="true"


Related articles: