In 2020, this is the way to do it. You'll need to pass what you need to as part of the referral URL. The code below uses SharedPreferences
to store a flag so it'll only process the referral URL once.
in your build.gradle
file
dependencies {
....
implementation 'com.android.installreferrer:installreferrer:2.1'
}
And in your AndroidApplication
import android.content.SharedPreferences;
import android.os.RemoteException;
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
import com.badlogic.gdx.backends.android.AndroidApplication;
public class YourApplication extends AndroidApplication {
public static final String REFERRAL_PROCESSED = "referral-processed";
private void checkInstallReferralLink() {
final SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean(REFERRAL_PROCESSED, false))
return;
final InstallReferrerClient referrerClient;
referrerClient = InstallReferrerClient.newBuilder(this).build();
referrerClient.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
ReferrerDetails response;
try {
response = referrerClient.getInstallReferrer();
if (response != null) {
String referrerUrl = response.getInstallReferrer();
if (referrerUrl != null) {
processReferralUrl(referrerUrl);
}
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(REFERRAL_PROCESSED, true);
editor.apply();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
break;
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
}
});
}
private void processReferralUrl(String referrerUrl) {
// Do what you need to here
}
}