Skip to content

Instantly share code, notes, and snippets.

@aaustin
Last active December 24, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaustin/af1a4302d6389ff36d30ec495a0647ce to your computer and use it in GitHub Desktop.
Save aaustin/af1a4302d6389ff36d30ec495a0647ce to your computer and use it in GitHub Desktop.
How to handle deep link routing in Android apps with in-app browsers
// List of common browser package names
String[] browserPackageNames = {"com.android.chrome", "org.mozilla.firefox", "com.UCMobile.intl", "com.sec.android.app.sbrowser", "com.opera.browser", "com.opera.mini.native", "com.microsoft.emmx"};
];
void loadURL(Context context, URI destinationUrl) {
boolean isAppOpened = false;
try {
// First, check if the URI can resolve an intent
Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(destinationUrl);
ResolveInfo activity = intent.resolveActivity(context.getPackageManager());
// Then check if the destination app is not a browser, because you want to load those in your own in-app browser
if (activity != null && !Arrays.asList(browserPackageNames).contains(activity.activityInfo.packageName)) {
// If it is a 3rd party app, then open it up!
context.startActivity(intent);
isAppOpened = true;
}
} catch (Exception ignore) { }
// If no app was opened via an intent, load the URL in your custom in-app browser.
if (!isAppOpened) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(context, destinationUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment