Retrieving the URL from Custom Tabs in Android

Author profile picture
In my earlier article I equipped an summary of Custom Tabs, or Chrome Custom Tabs, in Android. In brief, this element permits your app to seamlessly transition between application and internet content material and it’s utilized by a number of widely known programs together with Twitter, The Gaurdian, Tumblr, Stack Overflow, and Feedly. They’re protected, easy to make use of, and their talent to preemptively load URLS makes them extremely speedy in comparison to possible choices like WebViews. With this mentioned, the usage of Custom Tabs is the most popular choice if you wish to give customers get right of entry to to the internet with out sending them to some other app. You’ll be able to be up and working with a couple of strains of code.
This all sounds lovely handy and relying on how you propose to make use of the browser, it could be. The most important inconvenience that I’ve came upon is that the Custom Tab’s present URL isn’t despatched again to the host application and retrieving it isn’t the maximum intuitive procedure. It seems that this habits used to be installed position to safegaurd the consumer’s privateness and that is sensible…however what if you wish to put in force a customized proportion button? Or you need to cause an motion when the consumer navigates to a selected web page? No longer all of the use circumstances for URL monitoring are right away obtrusive however a large number of folks have inquired about this:

It seems that is conceivable!

Intents are the Answer

Earlier than I am getting too forward of myself, I’ll admit that I haven’t discovered a solution to passively observe the consumer’s URL. You’ll be able to use a CustomTabsCallback to decide when pages get started and end loading however the callback does now not comprise any details about the precise URL. Nevertheless, if you’ll be able to get the consumer to click on on an motion button or menu choice, the URL is yours for the taking!

The usage of PendingIntent.getActivity

This segment of the Chrome Custom Tabs article explains that it’s conceivable to get the URL from a PendingIntent by way of calling Intent.getDataString() when the intent is won. We will be able to do this with a PendingIntent that begins our primary task.

Create an motion button and upload it in your CustomTabsIntent builder:

 val sendLinkIntent = Intent(primary, sendLinkIntent.putExtra(Intent.EXTRA_SUBJECT,"This is the hyperlink you had been exploring") val pendingIntent = PendingIntent.getActivity(primary,0,sendLinkIntent,PendingIntent.FLAG_UPDATE_CURRENT) // Set the motion button AppCompatResources.getDrawable(primary, R.drawable.close_icon)?.let  // Create the Custom Tabs Intent and release the URL val customTabsIntent: CustomTabsIntent = builder.construct() customTabsIntent.launchUrl(primary, Uri.parse(url))

Then deal with the intent when it comes again in your task. I positioned this code in the onCreate() manner of my primary task:

// Take care of any conceivable intents ************************************************************************** // The URL is saved in the intent's knowledge val knowledge: Uri? = intent?.knowledge // Work out what to do in accordance with the intent kind if (intent?.kind?.startsWith("symbol/") == true)  // Take care of intents with symbol knowledge ... Log.d("Intent",intent?.kind.toString())  else if (intent?.kind == "textual content/undeniable") else
As you’ll be able to see on this video, the construct above will ship the consumer again to the primary task at the side of the URL that they had been final viewing. This is able to paintings neatly if you wish to proportion the hyperlink or reserve it for later.

My factor with this system is that the redirection might come as an undesirable interruption to the consumer’s informal surfing. Why shouldn’t the consumer be capable to save a hyperlink and proceed their journey throughout the internet?

The usage of PendingIntent.getBroadcast()

Grabbing the present URL and final in the Custom Tab browser may also be completed with a BroadcastReceiver. In case you don’t have a lot revel in with those, take a look at this information sooner than proceeding on. Another way, let’s start.

Lengthen the BroadcastReceiver magnificence and position your code to deal with the incoming intent inside of the onRecieve() manner:

magnificence DigBroadcastReceiver() : BroadcastReceiver() 

Checklist the new BroadcastReceiver to your manifest record. This will get nested one layer below the<application> tag and the “identify” characteristic is the record identify together with all folders after your package deal identify. Right here’s my undertaking construction:

And right here’s the receiver in the manifest record:

<application> <receiver android:identify=".ui.dig.DigBroadcastReceiver" android:enabled="true" />

After getting that finished, create the PendingIntent that may ship out the broadcast and upload the related motion button in your CustomTabsIntent.Builder:

 val sendLinkIntent = Intent(primary,DigBroadcastReceiver() sendLinkIntent.putExtra(Intent.EXTRA_SUBJECT,"This is the hyperlink you had been exploring") val pendingIntent = PendingIntent.getBroadcast(primary,0,sendLinkIntent,PendingIntent.FLAG_UPDATE_CURRENT) // Set the motion button AppCompatResources.getDrawable(primary, R.drawable.close_icon)?.let  DrawableCompat.setTint(it, Colour.WHITE) builder.setActionButton(it.toBitmap(),"Upload this hyperlink in your dig",pendingIntent,false)  val customTabsIntent: CustomTabsIntent = builder.construct() customTabsIntent.launchUrl(primary, Uri.parse(url))
Now whilst you press the motion button inside the Custom Tab, the present URL is retrieved and displayed in a Toast. That is a lot much less annoying to the total consumer revel in however moderately frankly, now not too helpful to us as the developer. Since the Broadcast Receiver we created doesn’t find out about the primary task, the whole lot that occurs inside the receiver is remoted (as an example, all of the code to generate and customise the Toast is in the Broadcast Receiver magnificence above).

If we need to in truth make the most of the URL knowledge, we will make the BroadcastReceiver acutely aware of the MainActivity by way of nesting the new magnificence inside of a fraction or task. The <receiver> tag in the manifest record will wish to be up to date accordingly (see underneath). I’ve equipped each eventualities since some folks might need to prepare their categories otherwise.

 <receiver android:identify=".ui.dig.DigTabs$DigBroadcastReceiver" android:enabled="true" />

In Conclusion

Extracting the present URL from an intent despatched by way of the Custom Tabs seems to be lovely easy when you don’t thoughts making customers click on a toolbar button. There nonetheless doesn’t appear to be a solution to passively retrieve this information however the yet-unused extras argument in the CustomTabsCallback magnificence might trade that. Till the, an additional click on right here and there must suffice.

I’ll do a little investigating and replace you quickly!

Take a look at the complete collection on Custom Tabs:


The Noonification banner

Subscribe to get your day by day round-up of most sensible tech tales!