如何将安装引用字符串传递给我的应用程序

How to pass install referrer string to my App

我已经在我的应用程序中实现了安装引荐来源网址机制,我将在 InstallReferrerReceiver::onReceive() 中收到它,就像这样:

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?
    }
}

据我所知,它是在安装完成后调用的,但应用程序尚未启动,因为一旦我在 GooglePlay 应用程序商店中单击 open,它就会完成。

那么如何将引用字符串传递到我的应用程序来处理它?

我试图将它存储在 defaultSharedPreferences 中,但是一旦应用程序是 运行,我就无法读取它。

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));
    }
}

但似乎 getDefaultSharedPreferences 指的是 InstallReferrerReceiver 中的受限上下文,与应用程序中使用的上下文不同。

那么如何解决这个问题呢? 我只是准确地说我将它用于私人参数,它与任何活动无关!

尝试使用以下方法在 SharedPrefs 中存储数据:

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