androidinstall-referrer

How to pass install referrer string to my App


I've implemented the install referrer mechanism in my app and I weel receive it in InstallReferrerReceiver::onReceive() like that:

public class InstallReferrerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        String referrer = intent.getStringExtra("referrer");

        Log.v(Constants.APP_TAG, "InstallReferrerReceiver: " + referrer);

        // And then how to pass referrer to my App?
    }
}

According to what I see, it's called just after the installation done but the app is not yet started as it will be done once I'll click on open in the GooglePlay appstore.

So how can I pass the referrer string to my app to process it?

I tried to store it in defaultSharedPreferences but I cannot read it once the application is running.

public class InstallReferrerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v(Constants.APP_TAG, "Version code: " + BuildConfig.VERSION_CODE);

        String referrer = intent.getStringExtra("referrer");

        Log.v(Constants.APP_TAG, "InstallReferrerReceiver: " + referrer);

        Log.v(Constants.APP_TAG, "Store referrer in " + PreferenceManager.getDefaultSharedPreferencesName(context.getApplicationContext()));
        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        sharedPreferences.edit().putString(Constants.REFERRAL, referrer).apply();
        Log.v(Constants.APP_TAG, "Stored referrer is " + sharedPreferences.getString(Constants.REFERRAL, null));
    }
}

But it seems getDefaultSharedPreferences refers to a restricted context in InstallReferrerReceiver, different from the one used in the application.

So how do solve this problem? I just precise I use it for private parameters, it's not regarding any Campaign!


Solution

  • Try to store data in SharedPrefs using:

     SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
     Editor preferencesEditor = preferences.edit();
     preferencesEditor.putString(Constants.REFERRAL, referrer);
     preferencesEditor.commit();