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...

Solution of Image not Loaded or Auto Suggestions not working in Android Studio

Sometimes we face issue when some images are not loaded or showing in Android Studio . It shows that image not loaded try to open it externally to fix format problem .It cause when some gradle files are corrupted. also if you try auto suggestions not working in xml layout files like  you can try Invalidate Caches and Restart in Android Studio's File Menu option but you will get no result . So here are some steps to follow to remove this type of problem: Step 1: Close your Android Studio Completely. Go to your User Folder - on Windows 7/8 this would be: [SYSDRIVE]:\Users[your username] (ex. C:\Users\DroidMedium\) there you find a folder with name .AndroidStudio3.5  *( i am using Android Studio 3.5.1 version your version may be different according to your Android Studio version) open this folder now you will find two folders Go to System folder  C:\Users\DroidMedium\.AndroidStudio3.5\System\ Step 2: Now under System Folder you will find Caches folder Delete the Folder Com...