android - 从 Google Play 商店安装应用程序后如何打开特定 Activity 以获得深层链接

标签 android android-intent deeplink

在我从 Play 商店安装我的应用程序后,是否有任何可能的方法打开某些特定的 Activity/页面,Play 商店的 url 来自深层链接(在这种情况下,深层链接/url 来自 QR 代码)?

我期望用户将根据深层链接被定向到特定页面

最佳答案

2020年,这就是做事的方式。您需要将需要的内容作为推荐 URL 的一部分进行传递。下面的代码使用 SharedPreferences 来存储一个标志,因此它只会处理一次推荐 URL。

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

关于android - 从 Google Play 商店安装应用程序后如何打开特定 Activity 以获得深层链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55369717/

相关文章:

java - 尝试通过通知实现警报

android - 通过 web 链接/html/web 应用程序启动安装的第三方 android 应用程序?

android - Google PlayStore 的 Alpha/Beta 测试

android - parseSdkContent 中的未知异常

android - 如何在 Android 中不指定任何音乐文件的情况下启动 MusicPlayer

firebase - 如何在 Firebase 动态链接中编辑深层链接参数

android - 为什么在我使用自定义主机和架构时 Deeplink 不起作用?

android - 自上次项目同步以来,Gradle 文件已更改。 IDE 正常工作可能需要项目同步

android - 哪个 Eclipse 仅适用于 Android?

java - 显示 Android 上的所有广播事件