Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction.

License

NotificationsYou must be signed in to change notification settings

braintree/browser-switch-android

Repository files navigation

GitHub Actions Tests

Android Browser Switch makes it easy to open a url in a browser orChrome Custom Tab and receive aresponse as the result of user interaction, either cancel or response data from the web page.

Setup

Add the library to your dependencies in yourbuild.gradle:

dependencies {  implementation'com.braintreepayments.api:browser-switch:3.3.0'}

To preview the latest work in progress builds, add the following SNAPSHOT dependency in yourbuild.gradle:

dependencies {  implementation'com.braintreepayments.api:browser-switch:3.3.1-SNAPSHOT'}

You will also need to add the Sonatype snapshots repo to your top-levelbuild.gradle to import SNAPSHOT builds:

allprojects {    repositories {        maven {            url'https://oss.sonatype.org/content/repositories/snapshots/'        }    }}

AndroidManifest.xml

Declare an activity that you own as a deep link target in yourAndroidManifest.xml:

<activityandroid:name="com.myapp.MyDeepLinkTargetActivity"android:exported="true">    <intent-filter>        <actionandroid:name="android.intent.action.VIEW"/>        <dataandroid:scheme="my-custom-url-scheme"/>        <categoryandroid:name="android.intent.category.DEFAULT"/>        <categoryandroid:name="android.intent.category.BROWSABLE"/>    </intent-filter></activity>

Note: The scheme you define must use all lowercase letters. This is due toscheme matching on the Android framework being case sensitive, expecting lower case. The scheme must also bevalid according to RFC 2396.

If these requirements are not met, an error will be returned and no browser switch will occur.

Usage

A browser switch can be initiated by callingBrowserSwitchClient#start(). UseBrowserSwitchOptions to configure options for browser switching:

val browserSwitchOptions=BrowserSwitchOptions().apply {    requestCode=MY_REQUEST_CODE    url="https://site-to-load.com?callbackURL=my-custom-url-scheme%3A%2F%2Fsuccess"    returnUrlScheme="my-custom-url-scheme"}when (val pendingRequest= browserSwitchClient.start(this, browserSwitchOptions)) {isBrowserSwitchPendingRequest.Started-> {// store pending request    }isBrowserSwitchPendingRequest.Failure-> {// browser was unable to be launched, handle failure    }}

In the above example, notice the encodedcallbackURL parameter is forwarded to the website that will be loaded. The callback url must have the same custom scheme set inBrowserSwitchOptions. When this URL is loaded by the site, the Android OS will re-direct the user to the deep link destinationActivity defined in theAndroidManifest.xml.

To capture a browser switch result, override your deep link targetActivity with the following code snippet:

overridefunonResume() {    handleReturnToAppFromBrowser(intent) }funhandleReturnToAppFromBrowser(intent:Intent) {// fetch stored pending request    fetchPendingRequestFromPersistentStorage()?.let { startedRequest->when (val browserSwitchResult= browserSwitchClient.parseResult(startedRequest, intent)) {isBrowserSwitchResult.Success-> {// handle successful browser switch result// clear stored pending request            }isBrowserSwitchResult.NoResult-> {// user did not complete browser switch// allow user to complete browser switch, or clear stored pending request            }        }    }}

Launch Modes

If your deep link targetActivity hasandroid:launchMode="singleTop",android:launchMode="singleTask", orandroid:launchMode="singleInstance", add the following code snippet to your deep link targetActivity in theonNewIntent method instead ofonResume:

overridefunonNewIntent(newIntent:Intent?) {super.onNewIntent(intent)    handleReturnToAppFromBrowser(intent) }

Versions

This SDK abides by our Client SDK Deprecation Policy. For more information on the potential statuses of an SDK check ourdeveloper docs.

Major version numberStatusReleasedDeprecatedUnsupported
3.x.xActiveMarch 2025TBATBA
2.x.xInactiveFebruary 2021March 2026March 2027
1.x.xUnsupportedJune 2020April 2022April 2023

Help

License

Android Browser Switch is open source and available under the MIT license. See theLICENSE file for more info.

About

Open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors15


[8]ページ先頭

©2009-2025 Movatter.jp