Last active
August 29, 2015 14:00
-
-
Save sangkyoonnam/11360673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReferrerReceiver extends BroadcastReceiver { | |
private static final String INSTALL_REFERRER_ACTION = "com.android.vending.INSTALL_REFERRER"; | |
private static final String PREF_REFERRER = "referrer"; | |
private static final String PREF_RAW_REFERRER = "raw_referrer"; | |
private static final String ENCODING = "UTF-8"; | |
public ReferrerReceiver() { | |
super(); | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
try { | |
if (intent != null | |
&& intent.getAction().equals(INSTALL_REFERRER_ACTION)) { | |
String rawReferrer = intent.getStringExtra(PREF_REFERRER); | |
if (rawReferrer != null) { | |
String referrer = URLDecoder.decode(rawReferrer, ENCODING); | |
ReferrerReceiver.setReferrer(context, referrer); | |
Uri referrerUri = Uri.parse("localhost?" + referrer); | |
if (referrerUri != null) { | |
String value = referrerUri.getQueryParameter("[KEY]"); | |
} | |
} | |
} | |
} | |
catch (Exception e) { | |
} | |
} | |
public static void setReferrer(Context context, String referrer) { | |
SharedPreferences pref = context.getSharedPreferences( | |
PREF_REFERRER, Context.MODE_PRIVATE); | |
Editor editor = pref.edit(); | |
editor.putString(PREF_RAW_REFERRER, referrer); | |
editor.commit(); | |
} | |
public static String getReferrer(Context context) { | |
SharedPreferences pref = context.getSharedPreferences( | |
PREF_REFERRER, Context.MODE_PRIVATE); | |
return pref.getString(PREF_RAW_REFERRER, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment