Android SDK

What is the minimum required Android version?

The minimum Android version required is 21. Also, the compile SDK version version for android is 34 (But you should generally compile with the latest version)


Include CX SDK into your project

arrow_rightIntegration From Maven Repository

                The SDK is available on the central Maven repository. Add it in your root settings.gradle.
                    dependencyResolutionManagement {
	                    repositories {
		                    ...
		                    maven { url 'https://jitpack.io' }
	                    }
                    }
            
            
                Add a reference to the latest version of QuestionPro SDK in your app's build.gradle. 
                    dependencies {
                        implementation 'com.github.surveyanalyticscorp:android-cx:2.3.2'
                    }
                    

Modify your manifest

arrow_rightAdd following permissions

                      <uses-permission android:name="android.permission.INTERNET"/>
                      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
                

Usage

arrow_right Initialization of SDK

                    Initialise the SDK in your Application class, not in an Activity. This ensures the SDK is ready before any screen is shown and avoids repeated initialisation across Activity lifecycle events.
                    Create a custom Application class if you don't already have one, register it in AndroidManifest.xml with android:name=".MyApplication", and call QuestionProCX.getInstance().init(...) inside onCreate().
                    Initialising inside MainActivity or a base Activity is not recommended — it ties initialisation to the Activity lifecycle, which means it can run multiple times and will fail if the app is launched via a push notification or deep link that bypasses the main screen.
                    
                        TouchPoint touchPoint=new TouchPoint.Builder(DataCenter.US, "your_api_key")
                        .setPlatform(Platform.ANDROID)
                        .build();
                        
                        QuestionProCX.getInstance().init(this, touchPoint, new IQuestionProInitCallback() {
                        	@Override
	                        public void onInitializationSuccess(String message) {
		                        Log.d(TAG, "onSuccess: "+message);
	                        }
	                        
	                        @Override
	                        public void onInitializationFailure(String error) {
		                        Log.d(TAG, "onFailed: "+error);
	                        }
	                
	                        @Override
                            public void onError(int interceptId, String errorMessage) {
    		                    Log.e(TAG, "error=" + errorMessage);
                            }

                        });
                        
                    (TouchPoint constructor requires Data Center and API key as a default parameter)
                        
                    
arrow_right View Count rule

                    The 'View Count' rule, found in the 'Rules set-up' section of the admin dashboard, allows administrators to control 
                    when an intercept is displayed to users based on their mobile application navigation. 
                    This rule uses two parameters: screen_name to specify the screen(s) to monitor, and count to set the view threshold before the intercept is triggered. 

                    Set up the rule:
                    - Select the rule ‘view count’ from the mobile intercept settings.
                    - Set ‘screen name’ for example ‘checkout_screen’.
                    - Set the count (After how many events you want to launch the survey) 

                        
                    Configure it in the mobile application:
                        Add the below line of code to the app where you have to record the count. SDK will log and maintain the count 
                        and launch the survey once it satisfies the rule setup for the intercept.

                    
                    import com.questionpro.cxlib.QuestionProCX;
                    QuestionProCX.getInstance().setScreenVisited("checkout_screen");
                    
                    
arrow_right Custom Variables or Data Mapping usage

                    The Mobile Intercept SDK leverages Custom Variables to enrich the context and user details associated with an intercept. 

                    How to Set Data Mapping for an Intercept 
                    - Navigate to the intercept for which you want to configure data mapping.
                    - Click on the Add button to begin setting up the data mapping.

                    How to Set Data Mapping in SDK 
                    Data mapping is structured as a HashMap, where each entry consists of a String key and a String value. 
                    After initializing the SDK, your application can set data from anywhere within the app.

                    
                    import com.questionpro.cxlib.QuestionProCX;
                    import java.util.HashMap;

                    HashMap customVars = new HashMap<>();
                    customVars.put("firstName", "First Name");
                    customVars.put("surname", "Last Name");
                    customVars.put("emailAddress", "+918977234");

                    QuestionProCX.getInstance().setDataMappings(customVars);

                    
                    
arrow_right Get Survey URL

                    If you set up the Intercept interaction type to ‘Survey URL’, this callback function will return the survey URL 
                    instead of launching it. 
                    The consumer app will use this survey URL as per their requirement.

                    
                    import com.questionpro.cxlib.QuestionProCX;
                    import com.questionpro.cxlib.interfaces.IQuestionProCallback;

                    QuestionProCX.getInstance().getSurveyUrl(new IQuestionProCallback() {
                        @Override
                        public void getSurveyUrl(String surveyUrl) {
                            Log.d(TAG,"Survey url: "+surveyUrl);
                        }
                    });

                    
                    
arrow_right Close the survey window

                    If you want to close the survey window/screen programmatically before completing the survey, you can use the following

                    
                    import com.questionpro.cxlib.QuestionProCX;
                    QuestionProCX.getInstance().closeSurveyWindow();