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

How to Implement Item Click Interface in Android?

In Android development, creating interactive lists is essential to most apps. Often, these lists include items that users can click on to trigger actions such as opening a new screen or displaying more information. In this tutorial, we’ll walk through how to implement an item click interface in Android using Java and XML. By the end of this guide, you’ll know how to create a RecyclerView with an item click listener that responds to user taps. This approach is widely used in modern Android development because RecyclerView is both flexible and efficient. Step 1: Set Up Your Android Project First, create a new Android project in Android Studio. Make sure to choose Java as the programming language. Step 2: Add Dependencies Make sure that you have the required dependencies for RecyclerView in your build.gradle file. If not, add them like this: dependencies {     implementation 'androidx.recyclerview:recyclerview:1.2.1' } Sync the project after adding the dependency. Step 3: Define ...

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