In Many Android Application you can see their is refer Scheme Play Store url of app with some value and on other device we will get referral code or we can can what is the source of url coming from .So to achieve this we have some steps to follow :
for example the url is :
https://play.google.com/store/apps/details?id=com.droidmedium&referrer=DroidMedium1234
by clicking this url user redirect to Play Store and after installation we have to get the referral value "DroidMedium1234"
Step 1:
Create a class Name RefererDataReceiver.java
package com.droidmedium;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class RefererDataReciever extends BroadcastReceiver {
public static final String ACTION_UPDATE_DATA = "ACTION_UPDATE_DATA";
private static final String ACTION_INSTALL_REFERRER = "com.android.vending.INSTALL_REFERRER";
private static final String KEY_REFERRER = "referrer";
public static final String REFERRER_DATA = "REFERRER_DATA";
public RefererDataReciever() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Log.e("RefererDataReciever", "Intent is null");
return;
}
if (!ACTION_INSTALL_REFERRER.equals(intent.getAction())) {
Log.e("RefererDataReciever", "Wrong action! Expected: " + ACTION_INSTALL_REFERRER + " but was: " + intent.getAction());
return;
}
Bundle extras = intent.getExtras();
if (intent.getExtras() == null) {
Log.e("RefererDataReciever", "No data in intent");
return;
}
//TODO get referer from here
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
if (!sp.contains(REFERRER_DATA)) {
sp.edit().putString(REFERRER_DATA, (String) extras.get(KEY_REFERRER)).apply();
}
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(ACTION_UPDATE_DATA));
}
}
Step 2:
Add these lines in AndroidManifest.xml
<receiver
android:name=".RefererDataReciever"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Step 3:
get the referral value by using this code MainActivity.java
package com.droidmedium;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import static com.droidmedium.RefererDataReciever.REFERRER_DATA;
//YouTube URL :
public class MainActivity extends AppCompatActivity {
private final BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Fetched Value is : ")
.setMessage("Recieved Data is : "+getReferer(MainActivity.this))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText("Fetched value is "+getReferer(MainActivity.this));//DroidMedium1234
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mUpdateReceiver);
super.onPause();
}
@Override
protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(mUpdateReceiver, new IntentFilter(RefererDataReciever.ACTION_UPDATE_DATA));
super.onResume();
}
public static String getReferer(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
if (!sp.contains(REFERRER_DATA)) {
return "Didn't got any referrer)";
}
return sp.getString(REFERRER_DATA, null);
}
}
Reference Link: https://github.com/coderminion/AndroidReferrer
for testing the Application the App must be on Play Store to get Referral value .Thank you for reading this post.
Comments
Post a Comment