0

I'm trying to implement the following behavior into my Android App.

The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.

If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.

I tried to load the Android App link into Chrome and this way the app is opened.

My question at this point is how I can replicate the behavior of Chrome into my WebView??

I didn't find that much informations on this specific case besides the Android Documentation

Thanks for your help :)

Lupinixio
  • 41
  • 1
  • 7

2 Answers2

0

You can intercept the url click by using custom WebViewClient for the webview.

1)Set the custom webview client for the webview

2)Override the below method and return true (you can do this based on particular url also. )

shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

3)Override below method and handle the click and start the activity using either package name or playstore url with intent.

public void onLoadResource (WebView view, String url)

WebView mWebView; 



WebViewClient customWebClient = new WebViewClient(){
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
        return true;
    }
    
    @Override
    public void onLoadResource(WebView  view, String  url){
        if( url.equals("") ){
            // launch playstore activity
              final String appPackageName = "package name of the application"
              try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                  } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                  }
        }
    }
}
mWebView.setWebViewClient(customWebClient);
Satish Navada
  • 371
  • 1
  • 2
  • 9
  • The problem is that I don't know the package name of the app or the URL to intercept because are informations managed by the app I want to call. Like for Revolut for example this link https://oba.revolut.com should open the app and like this hundred more. If I try to open this link on Chrome it opens the app but I don't know how to achieve this automatic behavior on my custom webview. – Lupinixio Mar 08 '22 at 12:53
  • Can you provide some sample url's. – Satish Navada Mar 08 '22 at 14:29
  • You can still override webview client and override shouldOverrideUrlLoading method to use implicit intent which could open the page outside webview and could redirect to playstore or webpage. Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl()); – Satish Navada Mar 08 '22 at 15:31
  • The point is that I don't know the URL I should intercept because are declared in the Manifest of other apps – Lupinixio Mar 08 '22 at 17:21
  • You dont know have to know the url. You can intercept and let Android take care of loading the playstore or website. You just have to pass the url as it is to the android intent. Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl()); – Satish Navada Mar 09 '22 at 01:37
  • The point is that I don't want the system to open the website or the playstore, but only to open the corresponding app if installed or leave the user in the webview of the app if any matching app is found. – Lupinixio Mar 14 '22 at 15:14
0

In your WebViewClient override method shouldOverrideUrlLoading()

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                val url = request.url.toString()                  

                // start intent with URIs that contain all patterns that you need
                if (url.contains("needed-pattern") {
                    try {
                        ContextCompat.startActivity(
                            view.context,
                            Intent(Intent.ACTION_VIEW, Uri.parse(url)),
                            null
                        )
                    } catch (e: ActivityNotFoundException) {
                        Log.d("Clicked app is not installed!")
                    }

                    return true
                }


                return false
            }