Skip to main content

How to use Google Play Referral to get the Source

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



for testing the Application the App must be on Play Store to get Referral value .Thank you for reading this post. 

Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Working with Android 11 (Changes and Security Features)

 Hello everyone , I am here with new article which is hot topic nowadays "Android 11" .The stable version of Android.  Android 11 is the eleventh major release and 18th version of Android, the mobile operating system developed by the Open Handset Alliance led by Google. It was released on September 8, 2020.It is comes with many security features and other features as well . And it is now compulsory in play store  to upload new apps with API lavel 30 which is compatible with Android 11 and from November onwards old apps also have to update with API 30 .Some other guidelines you can check out from here . Play Store Guidelines So its clear that we have to update our apps with API level 30 .But Android 11 comes with some changes as well which we have to do in our projects. For example from Android developer site "Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. ". Scoped storage enforcement: Apps...

How to Disable Edge-to-Edge Mode in Android 15 and Android 16

How to Disable Edge-to-Edge Mode in Android 15 and Android 16 How to Disable Edge-to-Edge Mode in Android 15 and Android 16 With each new Android version, Google continues to enhance UI experiences by embracing modern, immersive design principles. Starting with Android 10, edge-to-edge mode has become the standard, allowing apps to extend their content beneath system bars for a more immersive display. In Android 15 and the upcoming Android 16 releases, edge-to-edge mode remains a core part of the UI framework, leveraging gestures and providing richer visual experiences. However, as developers, there are times when we may want to disable edge-to-edge mode — whether for legacy app compatibility, ensuring content fidelity, or managing complex UI layouts. In this detailed guide, we'll explore how you can disable edge-to-edge mode in Android 15 and Android 16. We will cover the background of edge-to-edge, why you might want to di...