Last active
December 24, 2018 22:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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