Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open specific activity after install an app from Google Play Store for a deep link

is there any possible way to open some specific activity/page after I installed my apps from play store, the url of play store come from deeplink (on the case, the deeplink/url come from QR Code) ?

i am expecting that user will be directed to specific page based on the deeplink

like image 732
gilllsss Avatar asked Dec 06 '25 19:12

gilllsss


2 Answers

In 2020, this is the way to do it. You'll need to pass what you need to as part of the referral URL. The code below uses SharedPreferences to store a flag so it'll only process the referral URL once.

in your build.gradle file

dependencies {
    ....
    implementation 'com.android.installreferrer:installreferrer:2.1'
}

And in your AndroidApplication

import android.content.SharedPreferences;
import android.os.RemoteException;
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
import com.badlogic.gdx.backends.android.AndroidApplication;

public class YourApplication extends AndroidApplication {

    public static final String REFERRAL_PROCESSED = "referral-processed";

    private void checkInstallReferralLink() {
        final SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this);
        if (prefs.getBoolean(REFERRAL_PROCESSED, false))
            return;

        final InstallReferrerClient referrerClient;

        referrerClient = InstallReferrerClient.newBuilder(this).build();
        referrerClient.startConnection(new InstallReferrerStateListener() {
            @Override
            public void onInstallReferrerSetupFinished(int responseCode) {
                switch (responseCode) {
                    case InstallReferrerClient.InstallReferrerResponse.OK:
                        ReferrerDetails response;
                        try {
                            response = referrerClient.getInstallReferrer();
                            if (response != null) {
                                String referrerUrl = response.getInstallReferrer();
                                if (referrerUrl != null) {
                                    processReferralUrl(referrerUrl);
                                }
                            }

                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putBoolean(REFERRAL_PROCESSED, true);
                            editor.apply();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        break;
                    case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                    case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
                        break;
                }
            }

            @Override
            public void onInstallReferrerServiceDisconnected() {
            }
        });
    }

    private void processReferralUrl(String referrerUrl) {
        // Do what you need to here
    }
}
like image 80
Will Calderwood Avatar answered Dec 09 '25 19:12

Will Calderwood


To open the specific screen using Deep link, you need to implement the deeplink functionality and add specific screen in Manifest file. Please refer the below sample to implement the deeplink:

https://medium.com/@muratcanbur/intro-to-deep-linking-on-android-1b9fe9e38abd

like image 40
Google Avatar answered Dec 09 '25 19:12

Google



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!