Documentation > Android

Advanced Topics

Changing account credentials

If you’d not want to insert your account credentials in your app’s AndroidManifest.xml, or would like to change your account ID programmatically, you need to create a custom Application class (if you don’t have one already) with the following content:

import android.app.Application;
import com.clevertap.android.sdk.ActivityLifecycleCallback;
import com.clevertap.android.sdk.CleverTapAPI;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        CleverTapAPI.changeCredentials("Your account ID here", "Your account token here");
        ActivityLifecycleCallback.register(this); // Must be called before super.onCreate()
        super.onCreate();
    }
}
copy Copied

If you’ve just created this subclass of Application, update your AndroidManifest.xml to specify your subclass:

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:name=".MyApplication">
copy Copied

Manually updating user location

When permitted, the CleverTap SDK automatically collects and sets the device location. The application is responsible for requesting the user’s permission to use location. If this permission is granted, location is accessible to CleverTap.

On Android, location is provided to the application via the Android Location API. The Android Location API provides two options:

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION

Unless your application is dependent on extremely accurate location data, we recommend selecting the Coarse_Location option since it has better battery performance characteristics. The accuracy of course location is roughly one city block.

The location is updated in CleverTap each time the application is launched and each time the user performs an action in the App (or every 20 minutes if there is no activity in the App).

For more frequent location updates, the App can pass the location to the SDK using updateLocation:

clevertap = CleverTapAPI.getInstance(getApplicationContext());
clevertap.updateLocation(location); //android.location.Location
copy Copied

Using your existing activity lifecycle listener

If you’d like to use your own activity lifecycle listener, please update it to incorporate the following code:

application.registerActivityLifecycleCallbacks(
        new android.app.Application.ActivityLifecycleCallbacks() {
        
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                CleverTapAPI.setAppForeground(true);
                try {
                    CleverTapAPI.getInstance(application).event.pushNotificationEvent(activity.getIntent().getExtras());
                } catch (Throwable t) {
                    // Ignore
                }
                try {
                    Intent intent = activity.getIntent();
                    Uri data = intent.getData();
                    CleverTapAPI.getInstance(application).pushDeepLink(data);
                } catch (Throwable t) {
                    // Ignore
                }
            }
            
            @Override
            public void onActivityStarted(Activity activity) {
            }
            
            @Override
            public void onActivityResumed(Activity activity) {
                try {
                    CleverTapAPI.getInstance(application).activityResumed(activity);
                } catch (Throwable t) {
                    // Ignore
                }
            }
            
            @Override
            public void onActivityPaused(Activity activity) {
                try {
                    CleverTapAPI.getInstance(application).activityPaused(activity);
                } catch (Throwable t) {
                    // Ignore
                }
            }
            
            @Override
            public void onActivityStopped(Activity activity) {
            }
            
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
            }
            
            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        }
);
copy Copied