How to add EXTRA_REFERRER to CustomTabsIntent builder in Chrome custom tab for Android

谁都会走 提交于 2020-08-05 05:34:18

问题


I am using newly launched Chrome Custom tabs for android instead of using webviews. This is the link to their documentation

Here is the code that shows how to use it.

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

Question is that I want to add Intent.EXTRA_REFERRER to this. below is the para copied from their blog in their own words..

It's usually very important for websites to track where their traffic is coming from. Make sure you let them know you are sending them users by setting the referrer when launching your Custom Tab

intent.putExtra(Intent.EXTRA_REFERRER, 
             Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + context.getPackageName()));

I failed to figure out any intent created to launch custom tabs.. Where to add this line?? If somebody has came across this, help please.


回答1:


you can put the extra on the Intent that is inside the CustomTabsIntent created by the builder like the following:

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
        Uri.parse("android-app://" + context.getPackageName()));
customTabsIntent.launchUrl(this, Uri.parse(url));

Explanation: Under the hood, a Custom tab is opened by using regular a Intent with a set of Extras that configure the UI customization. It's possible to see more on how it works at the Low Level API section of the docs. When CustomTabsIntent.Builder#build() is called, it creates a CustomTabsIntent, with a properly configured Intent inside it. This intent is still exposed by the API and that's what we use to add the EXTRA_REFERRER.



来源:https://stackoverflow.com/questions/37937244/how-to-add-extra-referrer-to-customtabsintent-builder-in-chrome-custom-tab-for-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!