从Google Play商店安装应用后,如何打开特定活动以获得深层链接

时间:2019-03-27 04:15:04

标签: android android-intent deeplink

从Play商店安装我的应用程序后,是否有任何可能的方法来打开某些特定活动/页面,Play商店的网址来自Deeplink(在这种情况下,Deeplink /网址来自QR码)?

我希望用户会基于Deeplink定向到特定页面

3 个答案:

答案 0 :(得分:1)

在2020年,这就是做到这一点的方式。您需要将所需的内容传递给引荐网址。下面的代码使用SharedPreferences存储标志,因此它只会处理引荐网址一次。

在您的build.gradle文件中

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

在您的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
    }
}

答案 1 :(得分:0)

  

要使用深层链接打开特定屏幕,您需要实施   深层链接功能,并在清单文件中添加特定屏幕。   请参考以下示例以实现深度链接:

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

答案 2 :(得分:0)

您需要使用包含

的意图过滤器声明接收方
<action android:name="com.android.vending.INSTALL_REFERRER" />

然后通过键“引荐来源”从意图中获取数据

@Override
public void onReceive(Context context, Intent intent) {
    String rawReferrerString = intent.getStringExtra("referrer");
    if(rawReferrerString != null) {
        Log.i("MyApp", "Received the following intent " + rawReferrerString);
    }
}

https://developer.android.com/google/play/installreferrer