Class LDClient
- All Implemented Interfaces:
LDClientInterface,Closeable,AutoCloseable
init(Application, LDConfig, LDContext) method.
Like all client-side LaunchDarkly SDKs, the LDClient always has a single current
LDContext (evaluation context). You specify this context at initialization time, and you
can change it later with identify(LDContext). All subsequent calls to evaluation
methods like boolVariation(java.lang.String, boolean) refer to the flag values for the current context.
Normally, the SDK uses the exact context that you have specified in th LDContext.
However, you can also tell the SDK to generate a randomized identifier and use this as the
context's key; see LDConfig.Builder.generateAnonymousKeys(boolean).
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLDClient(com.launchdarkly.sdk.android.PlatformState platformState, IEnvironmentReporter environmentReporter, com.launchdarkly.sdk.android.TaskExecutor taskExecutor, com.launchdarkly.sdk.android.PersistentDataStoreWrapper.PerEnvironmentData environmentStore, LDContext initialContext, LDConfig config, String mobileKey, String environmentName) -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a hook to the client.allFlags()Returns a map of all feature flags for the current evaluation context.booleanboolVariation(String key, boolean defaultValue) Returns the boolean value of a feature flag for the current evaluation context.boolVariationDetail(String key, boolean defaultValue) Returns the boolean value of a feature flag for the current evaluation context, along with information about how it was calculated.voidclose()Closes the client.doubledoubleVariation(String key, double defaultValue) Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context.doubleVariationDetail(String key, double defaultValue) Returns the double-precision floating-point numeric value of a feature flag for the current evaluation context, along with information about how it was calculated.voidflush()Sends all pending events to LaunchDarkly.static LDClientget()Returns theLDClientinstance that was previously created withinit(Application, LDConfig, LDContext, int)orinit(Application, LDConfig, LDContext).Gets aConnectionInformationobject from the client representing the current state of the clients connection.static LDClientgetForMobileKey(String keyName) Returns theLDClientinstance that was previously created withinit(Application, LDConfig, LDContext, int)orinit(Application, LDConfig, LDContext), for a specific environment.Returns the version of the SDK, for instance "2.7.0".Changes the current evaluation context, requests flags for that context from LaunchDarkly if we are online, and generates an analytics event to tell LaunchDarkly about the context.Deprecated.static LDClientInitializes the singleton instance and blocks for up tostartWaitSecondsseconds until the client has been initialized.intintVariation(String key, int defaultValue) Returns the integer value of a feature flag for the current evaluation context.intVariationDetail(String key, int defaultValue) Returns the integer value of a feature flag for the current evaluation context, along with information about how it was calculated.booleanChecks whetherLDConfig.Builder.disableBackgroundUpdating(boolean)was set totruein the configuration.booleanReturns true if the client has successfully connected to LaunchDarkly and received feature flags afterinit(Application, LDConfig, LDContext, int)was called.booleanChecks whether the client has been put into offline mode.jsonValueVariation(String key, LDValue defaultValue) Returns the value of a feature flag for the current evaluation context, which may be of any type.jsonValueVariationDetail(String key, LDValue defaultValue) Returns the value of a feature flag for the current evaluation context, which may be of any type, along with information about how it was calculated.voidregisterAllFlagsListener(LDAllFlagsListener allFlagsListener) Registers aLDAllFlagsListenerto be called when a flag update is processed by the SDK.voidregisterFeatureFlagListener(String flagKey, FeatureFlagChangeListener listener) Registers aFeatureFlagChangeListenerto be called when theflagKeychanges from its current value.voidregisterStatusListener(LDStatusListener statusListener) Registers aLDStatusListenerto be called on connection status updates.voidShuts down any network connections maintained by the client and puts the client in offline mode, preventing the client from opening new network connections untilsetOnline()is called.voidRestores network connectivity for the client, if the client was previously in offline mode.stringVariation(String key, String defaultValue) Returns the string value of a feature flag for the current evaluation context.stringVariationDetail(String key, String defaultValue) Returns the string value of a feature flag for the current evaluation context, along with information about how it was calculated.voidTracks that an application-defined event occurred.voidTracks that an application-defined event occurred, and provides additional custom data.voidtrackMetric(String eventName, LDValue data, double metricValue) Tracks that an application-defined event occurred, and provides an additional numeric value for custom metrics.voidunregisterAllFlagsListener(LDAllFlagsListener allFlagsListener) Unregisters aLDAllFlagsListenerso it will no longer be called on flag updates.voidunregisterFeatureFlagListener(String flagKey, FeatureFlagChangeListener listener) Unregisters aFeatureFlagChangeListenerfor theflagKey.voidunregisterStatusListener(LDStatusListener statusListener) Unregisters aLDStatusListenerso it will no longer be called on connection status updates.
-
Constructor Details
-
LDClient
protected LDClient(@NonNull com.launchdarkly.sdk.android.PlatformState platformState, @NonNull IEnvironmentReporter environmentReporter, @NonNull com.launchdarkly.sdk.android.TaskExecutor taskExecutor, @NonNull com.launchdarkly.sdk.android.PersistentDataStoreWrapper.PerEnvironmentData environmentStore, @NonNull LDContext initialContext, @NonNull LDConfig config, @NonNull String mobileKey, @NonNull String environmentName) throws LaunchDarklyException - Parameters:
platformState- the platform stateenvironmentReporter- the environment reportertaskExecutor- the task executorenvironmentStore- the environment storeinitialContext- initial contextconfig- the configmobileKey- the mobile keyenvironmentName- the environment name- Throws:
LaunchDarklyException- ifLDClientcannot be created
-
-
Method Details
-
init
@Deprecated public static Future<LDClient> init(@NonNull android.app.Application application, @NonNull LDConfig config, @NonNull LDContext context) Deprecated.Initializing theLDClientwithout a timeout is no longer permitted to help prevent consumers from blocking their application execution by mistake when connectivity is poor. Please useinit(Application, LDConfig, LDContext, int)and specify a timeout instead.Initializes the singleton/primary instance. The result is aFuturewhich will complete once the client has been initialized with the latest feature flag values. For immediate access to the Client (possibly with out of date feature flags), it is safe to ignore the return value of this method, and afterward callget()If the client has already been initialized, is configured for offline mode, or the device is not connected to the internet, this method will return a
Futurethat is already in the completed state.- Parameters:
application- your Android applicationconfig- configuration used to set up the clientcontext- the initial evaluation context; seeLDClientfor more information about setting the context and optionally requesting a unique key for it- Returns:
- a
Futurewhich will complete once the client has been initialized - Since:
- 3.0.0
- See Also:
-
init
public static LDClient init(android.app.Application application, LDConfig config, LDContext context, int startWaitSeconds) Initializes the singleton instance and blocks for up tostartWaitSecondsseconds until the client has been initialized. If the client does not initialize withinstartWaitSecondsseconds, it is returned anyway and can be used, but may not have fetched the most recent feature flag values.- Parameters:
application- your Android applicationconfig- configuration used to set up the clientcontext- the initial evaluation context; seeLDClientfor more information about setting the context and optionally requesting a unique key for itstartWaitSeconds- maximum number of seconds to wait for the client to initialize. Determines when this function will return if no flags have been fetched. If you use a large timeout, then any network delays could cause your application to wait a long time before continuing execution.- Returns:
- the primary LDClient instance
- Since:
- 3.0.0
- See Also:
-
get
Returns theLDClientinstance that was previously created withinit(Application, LDConfig, LDContext, int)orinit(Application, LDConfig, LDContext).If you have configured multiple environments, this method returns the instance for the primary environment.
- Returns:
- the singleton instance
- Throws:
LaunchDarklyException- ifinithas not been called- See Also:
-
getForMobileKey
Returns theLDClientinstance that was previously created withinit(Application, LDConfig, LDContext, int)orinit(Application, LDConfig, LDContext), for a specific environment.This method is only relevant if you have configured multiple environments with
LDConfig.Builder.secondaryMobileKeys(Map).- Parameters:
keyName- the name you gave to this environment (this must be one of the keys in the map you passed toLDConfig.Builder.secondaryMobileKeys(Map))- Returns:
- the singleton instance for the environment associated with the given name
- Throws:
LaunchDarklyException- ifinithas not been called- See Also:
-
trackMetric
Description copied from interface:LDClientInterfaceTracks that an application-defined event occurred, and provides an additional numeric value for custom metrics.This method creates a "custom" analytics event containing the specified event name (key) the current evaluation context, optional custom data, and a numeric metric value.
Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush().- Specified by:
trackMetricin interfaceLDClientInterface- Parameters:
eventName- the name of the eventdata- anLDValuecontaining additional data associated with the event; if not applicable, you may pass eithernullorLDValue.ofNull()metricValue- a numeric value used by the LaunchDarkly experimentation feature in numeric custom metrics; this field will also be returned as part of the custom event for Data Export- See Also:
-
trackData
Description copied from interface:LDClientInterfaceTracks that an application-defined event occurred, and provides additional custom data.This method creates a "custom" analytics event containing the specified event name (key) the current evaluation context, and optional custom data. To specify a numeric metric, use
LDClientInterface.trackMetric(String, LDValue, double)instead.Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush().- Specified by:
trackDatain interfaceLDClientInterface- Parameters:
eventName- the name of the eventdata- anLDValuecontaining additional data associated with the event; if not applicable, you may pass eithernullorLDValue.ofNull()- See Also:
-
track
Description copied from interface:LDClientInterfaceTracks that an application-defined event occurred.This method creates a "custom" analytics event containing the specified event name (key) and the current evaluation context. You may attach other data to the event by calling
LDClientInterface.trackData(String, LDValue)orLDClientInterface.trackMetric(String, LDValue, double)instead.Note that event delivery is asynchronous, so the event may not actually be sent until later; see
LDClientInterface.flush().- Specified by:
trackin interfaceLDClientInterface- Parameters:
eventName- the name of the event- See Also:
-
identify
Description copied from interface:LDClientInterfaceChanges the current evaluation context, requests flags for that context from LaunchDarkly if we are online, and generates an analytics event to tell LaunchDarkly about the context.If the SDK is online, the returned
Futureis completed once the SDK has received feature flag values for the new context from LaunchDarkly, or received an unrecoverable error. If the SDK is offline, the returnedFutureis completed immediately.The SDK normally caches flag settings for recently used evaluation contexts; this behavior can be configured with
LDConfig.Builder.maxCachedContexts(int).- Specified by:
identifyin interfaceLDClientInterface- Parameters:
context- the new evaluation context; seeLDClientfor more about setting the context and optionally requesting a unique key for it- Returns:
- a Future whose success indicates the flag values for the new evaluation context have been stored locally and are ready for use
-
allFlags
Description copied from interface:LDClientInterfaceReturns a map of all feature flags for the current evaluation context. No events are sent to LaunchDarkly.- Specified by:
allFlagsin interfaceLDClientInterface- Returns:
- a map of all feature flags
-
boolVariation
Description copied from interface:LDClientInterfaceReturns the boolean value of a feature flag for the current evaluation context.If the flag variation does not have a boolean value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKeydoes not match any existing flag),defaultValueis returned.- Specified by:
boolVariationin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
boolVariationDetail
Description copied from interface:LDClientInterfaceReturns the boolean value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasonsto true withLDConfig.Builder.evaluationReasons(boolean). Otherwise, thereasonproperty of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
- Specified by:
boolVariationDetailin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag (seeLDClientInterface.boolVariation(String, boolean))- Returns:
- an
EvaluationDetailobject containing the value and other information
-
intVariation
Description copied from interface:LDClientInterfaceReturns the integer value of a feature flag for the current evaluation context.If the flag variation has a numeric value that is not an integer, it is rounded toward zero.
If the flag variation does not have a numeric value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKeydoes not match any existing flag),defaultValueis returned.- Specified by:
intVariationin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
intVariationDetail
Description copied from interface:LDClientInterfaceReturns the integer value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasonsto true withLDConfig.Builder.evaluationReasons(boolean). Otherwise, thereasonproperty of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.intVariation(java.lang.String, int).- Specified by:
intVariationDetailin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag (seeLDClientInterface.intVariation(String, int))- Returns:
- an
EvaluationDetailobject containing the value and other information
-
doubleVariation
Description copied from interface:LDClientInterfaceReturns the double-precision floating-point numeric value of a feature flag for the current evaluation context.If the flag variation does not have a numeric value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKeydoes not match any existing flag),defaultValueis returned.- Specified by:
doubleVariationin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
doubleVariationDetail
Description copied from interface:LDClientInterfaceReturns the double-precision floating-point numeric value of a feature flag for the current evaluation context, along with information about how it was calculated. Note that this will only work if you have setevaluationReasonsto true withLDConfig.Builder.evaluationReasons(boolean). Otherwise, thereasonproperty of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.doubleVariation(java.lang.String, double).- Specified by:
doubleVariationDetailin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag (seeLDClientInterface.doubleVariation(String, double))- Returns:
- an
EvaluationDetailobject containing the value and other information.
-
stringVariation
Description copied from interface:LDClientInterfaceReturns the string value of a feature flag for the current evaluation context.If the flag variation does not have a string value, or if an error makes it impossible to evaluate the flag (for instance, if
flagKeydoes not match any existing flag),defaultValueis returned.- Specified by:
stringVariationin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value
-
stringVariationDetail
Description copied from interface:LDClientInterfaceReturns the string value of a feature flag for the current evaluation context, along with information about how it was calculated.Note that this will only work if you have set
evaluationReasonsto true withLDConfig.Builder.evaluationReasons(boolean). Otherwise, thereasonproperty of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.stringVariation(java.lang.String, java.lang.String).- Specified by:
stringVariationDetailin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag (seeLDClientInterface.stringVariation(String, String))- Returns:
- an
EvaluationDetailobject containing the value and other information.
-
jsonValueVariation
Description copied from interface:LDClientInterfaceReturns the value of a feature flag for the current evaluation context, which may be of any type.The type
LDValueis used to represent any of the value types that can exist in JSON. UseLDValuemethods to examine its type and value.- Specified by:
jsonValueVariationin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag- Returns:
- value of the flag or the default value. Result will never be null, but may be LDValue#ofNull()
-
jsonValueVariationDetail
public EvaluationDetail<LDValue> jsonValueVariationDetail(@NonNull String key, LDValue defaultValue) Description copied from interface:LDClientInterfaceReturns the value of a feature flag for the current evaluation context, which may be of any type, along with information about how it was calculated.The type
LDValueis used to represent any of the value types that can exist in JSON. UseLDValuemethods to examine its type and value.Note that this will only work if you have set
evaluationReasonsto true withLDConfig.Builder.evaluationReasons(boolean). Otherwise, thereasonproperty of the result will be null.The evaluation reason will also be included in analytics events, if you are capturing detailed event data for this flag.
The behavior is otherwise identical to
LDClientInterface.jsonValueVariation(java.lang.String, com.launchdarkly.sdk.LDValue).- Specified by:
jsonValueVariationDetailin interfaceLDClientInterface- Parameters:
key- key for the flag to evaluatedefaultValue- default value in case of errors evaluating the flag (seeLDClientInterface.jsonValueVariation(String, LDValue))- Returns:
- an
EvaluationDetailobject containing the value and other information.
-
close
Closes the client. This should only be called at the end of a client's lifecycle.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- declared by the Closeable interface, but will not be thrown by the client
-
flush
public void flush()Description copied from interface:LDClientInterfaceSends all pending events to LaunchDarkly.- Specified by:
flushin interfaceLDClientInterface
-
isInitialized
public boolean isInitialized()Description copied from interface:LDClientInterfaceReturns true if the client has successfully connected to LaunchDarkly and received feature flags afterinit(Application, LDConfig, LDContext, int)was called. Also returns true if the SDK knows it will never be able to fetch flag data (such as when the client is set to offline mode or if in one shot configuration, the one shot fails). Otherwise this returns false until the client is able to retrieve latest feature flag data from LaunchDarkly services. This includes not connecting to LaunchDarkly within the start wait time provided toinit(Application, LDConfig, LDContext, int)even if the SDK has cached feature flags.- Specified by:
isInitializedin interfaceLDClientInterface- Returns:
- true if the client is able to retrieve flag data from LaunchDarkly or offline, false if the client has been unable to up to this point.
-
isOffline
public boolean isOffline()Description copied from interface:LDClientInterfaceChecks whether the client has been put into offline mode. This is true only ifLDClientInterface.setOffline()was called, or if the configuration hadLDConfig.Builder.offline(boolean)set to true, not if the client is simply offline due to a loss of network connectivity.- Specified by:
isOfflinein interfaceLDClientInterface- Returns:
- true if the client is in offline mode
-
setOffline
public void setOffline()Description copied from interface:LDClientInterfaceShuts down any network connections maintained by the client and puts the client in offline mode, preventing the client from opening new network connections untilsetOnline()is called.Note: The client automatically monitors the device's network connectivity and app foreground status, so calling
setOffline()orsetOnline()is normally unnecessary in most situations.- Specified by:
setOfflinein interfaceLDClientInterface
-
setOnline
public void setOnline()Description copied from interface:LDClientInterfaceRestores network connectivity for the client, if the client was previously in offline mode. This operation may be throttled if it is called too frequently.Note: The client automatically monitors the device's network connectivity and app foreground status, so calling
setOffline()orsetOnline()is normally unnecessary in most situations.- Specified by:
setOnlinein interfaceLDClientInterface
-
registerFeatureFlagListener
Description copied from interface:LDClientInterfaceRegisters aFeatureFlagChangeListenerto be called when theflagKeychanges from its current value. If the feature flag is deleted, thelistenerwill be unregistered.- Specified by:
registerFeatureFlagListenerin interfaceLDClientInterface- Parameters:
flagKey- the flag key to attach the listener tolistener- the listener to attach to the flag key- See Also:
-
unregisterFeatureFlagListener
Description copied from interface:LDClientInterfaceUnregisters aFeatureFlagChangeListenerfor theflagKey.- Specified by:
unregisterFeatureFlagListenerin interfaceLDClientInterface- Parameters:
flagKey- the flag key to remove the listener fromlistener- the listener to remove from the flag key- See Also:
-
isDisableBackgroundPolling
public boolean isDisableBackgroundPolling()Description copied from interface:LDClientInterfaceChecks whetherLDConfig.Builder.disableBackgroundUpdating(boolean)was set totruein the configuration.- Specified by:
isDisableBackgroundPollingin interfaceLDClientInterface- Returns:
- true if background polling is disabled
-
getConnectionInformation
Description copied from interface:LDClientInterfaceGets aConnectionInformationobject from the client representing the current state of the clients connection.- Specified by:
getConnectionInformationin interfaceLDClientInterface- Returns:
- An object representing the status of the connection to LaunchDarkly.
-
registerStatusListener
Description copied from interface:LDClientInterfaceRegisters aLDStatusListenerto be called on connection status updates.- Specified by:
registerStatusListenerin interfaceLDClientInterface- Parameters:
statusListener- the listener to be called on a connection status update
-
unregisterStatusListener
Description copied from interface:LDClientInterfaceUnregisters aLDStatusListenerso it will no longer be called on connection status updates.- Specified by:
unregisterStatusListenerin interfaceLDClientInterface- Parameters:
statusListener- the listener to be removed
-
registerAllFlagsListener
Description copied from interface:LDClientInterfaceRegisters aLDAllFlagsListenerto be called when a flag update is processed by the SDK.- Specified by:
registerAllFlagsListenerin interfaceLDClientInterface- Parameters:
allFlagsListener- the listener to be called with a list of flag keys on a flag update
-
unregisterAllFlagsListener
Description copied from interface:LDClientInterfaceUnregisters aLDAllFlagsListenerso it will no longer be called on flag updates.- Specified by:
unregisterAllFlagsListenerin interfaceLDClientInterface- Parameters:
allFlagsListener- the listener to be removed
-
getVersion
Description copied from interface:LDClientInterfaceReturns the version of the SDK, for instance "2.7.0".- Specified by:
getVersionin interfaceLDClientInterface- Returns:
- the version string
-
addHook
Description copied from interface:LDClientInterfaceAdd a hook to the client. In order to register a hook before the client starts, please use the `hooks` method ofLDConfig.Builder.Hooks provide entry points which allow for observation of SDK functions.
- Specified by:
addHookin interfaceLDClientInterface- Parameters:
hook- The hook to add.
-
LDClientwithout a timeout is no longer permitted to help prevent consumers from blocking their application execution by mistake when connectivity is poor.