Issue
I HAVE ALREADY TRIED How to handle intent:// on a webView URL? This solution just opens up a Google Play Store Web Page in my WebView, what I want is on the click, another app should open whose intent is provided as happens in Chrome.
My app has a WebView that works fine. At one stage, the WebView requests access to the following(an external payment app):
intent://pay/?pa=digitalshowroom1.payu@indus&pn=DOTPE%20PRIVATE%20LIMITED&tr=13261664955&tid=CX0FOrvSrHzDh7gP&am=1.00&cu=INR&tn=OrderId-CX0FOrvSrHzDh7gP
When I use the same website that my WebView is using in Chrome, it opens the external payment app successfully, i.e. Chrome is able to handle that intent, how can my app handle the same intent.
I seem to know I have to use
public boolean shouldOverrideUrlLoading(WebView view, String url)
for this, and I'm using it as follows:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("whatsapp://") || url.startsWith("tel") || url.startsWith("market"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else {
return false;
}
Now, this piece of code is handling the intents like opening WhatsApp, Calling app, etc. fine as they start with whatsapp
or tel
, so I'm able to leverage the url.startsWith()
method.
How can I handle the links that request access to external apps and start with intent://
?
Thanks in advance!
Solution
I tried something similar and found you have to build the URI like this and handle all URL parameters and call the build method. I did it for a UPI payments app. Call this method in shouldOverrideUrlLoading().
public void openPaymentApp(final String url)
{
try {
String pa= url.substring(url.indexOf("pa=")+3,url.indexOf("&pn"));
String pn=url.substring(url.indexOf("pn=")+3,url.indexOf("&tr"));
String tr=url.substring(url.indexOf("tr=")+3,url.indexOf("&tid"));
String tid=url.substring(url.indexOf("tid=")+4,url.indexOf("&am"));
String am=url.substring(url.indexOf("am=")+3,url.indexOf("&cu"));
String cu=url.substring(url.indexOf("cu=")+3,url.indexOf("&tn"));
String tn=url.substring(url.indexOf("tn=")+3,url.indexOf("#Intent"));
Uri uri =
new Uri.Builder()
.scheme("upi")
.authority("pay")
.appendQueryParameter("pa",pa)
.appendQueryParameter("pn",pn)
.appendQueryParameter("tr",tr)
.appendQueryParameter("tid",tid)
.appendQueryParameter("am",am)
.appendQueryParameter("cu",cu)
.appendQueryParameter("tn",tn)
.build();
Intent launchIntent = getPackageManager()
.getLaunchIntentForPackage( "com.package.name");
launchIntent.setData(uri);
startActivity( launchIntent );
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Coudln't open", Toast.LENGTH_SHORT).show();
}
}
Answered By - user15312910
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.