android - 如何使用 Google 的 installreferrer 库测试安装引荐来源网址?

标签 android google-play google-play-services install-referrer

有很多示例如何测试检测安装引用的“默认”方式,但没有示例如何测试 com.android.installreferrer:installreferrer 库。

例如

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER 
                       -n your.package.name/path.to.receiver --es referrer
                       --es referrer "EXTRA_STRING_VALUE"

不起作用,因为我们不知道接收者路径。那么如何测试呢?

最佳答案

InstallReferrerClientAndroidManifest.xml 中似乎没有注册任何 BroadcastReceiver。该库只是绑定(bind)到系统的安装引用服务...

private static final String SERVICE_PACKAGE_NAME = "com.android.vending";
private static final String SERVICE_NAME = "com.google.android.finsky.externalreferrer.GetInstallReferrerService";
private static final String SERVICE_ACTION_NAME = "com.google.android.finsky.BIND_GET_INSTALL_REFERRER_SERVICE";

客户端在手动安装后收到引荐来源网址utm_source=google-play&utm_medium=organic。没有公开 BroadcastReceiver(但 InstallReferrerService 应该有一个)。

原始Intent Bundle的键是:install_referrerreferrer_click_timestamp_secondsinstall_begin_timestamp_seconds code> 如果您想尝试模拟它 - 但 onInstallReferrerSetupFinished() 回调将间接传递结果。

文档还指出:

The install referrer information will be available for 90 days and won't change unless the application is reinstalled. To avoid unnecessary API calls in your app, you should invoke the API only once during the first execution after install. Your app can listen to the system broadcast Intent.ACTION_PACKAGE_FIRST_LAUNCH to identify the app's first execution.

<小时/>

所以这应该是action Intent.ACTION_PACKAGE_FIRST_LAUNCHintent-filter,它随后连接InstallReferrerClientInstallReferrerService。无法使用 adb 触发 Intent.ACTION_PACKAGE_FIRST_LAUNCH,因为它会过滤“ protected 广播操作字符串”,因此只有从 Play 商店安装时才会触发。

根据文档,实现可能看起来很相似:

AndroidManifest.xml:

<receiver
    android:name=".receiver.PackageStatusReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH"/>
    </intent-filter>
</receiver>

PackageStatusReceiver.java:

public class PackageStatusReceiver extends BroadcastReceiver implements InstallReferrerStateListener {

    protected static final String LOG_TAG = PackageStatusReceiver.class.getSimpleName();

    private InstallReferrerClient referrerClient;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction() != null) {
            if(intent.getAction().equals(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
                this.referrerClient = InstallReferrerClient.newBuilder(context).build();
                this.referrerClient.startConnection(this);
            }
        }
    }

    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                Log.d(LOG_TAG, "InstallReferrer Response.OK");
                try {
                    ReferrerDetails response = referrerClient.getInstallReferrer();
                    String referrer = response.getInstallReferrer();
                    long clickTimestamp = response.getReferrerClickTimestampSeconds();
                    long installTimestamp = response.getInstallBeginTimestampSeconds();
                    Log.d(LOG_TAG, "InstallReferrer " + referrer);
                    referrerClient.endConnection();
                } catch (RemoteException e) {
                    Log.e(LOG_TAG, "" + e.getMessage());
                }
                break;
            case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                Log.w(LOG_TAG, "InstallReferrer Response.FEATURE_NOT_SUPPORTED");
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_UNAVAILABLE");
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
                Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_DISCONNECTED");
                break;
            case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
                Log.w(LOG_TAG, "InstallReferrer Response.DEVELOPER_ERROR");
                break;
        }
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {
        Log.w(LOG_TAG, "InstallReferrer onInstallReferrerServiceDisconnected()");
    }
}

要测试这一点,您需要 Play 商店的引用链接,然后通过它们安装软件包...否则只会记录默认的引用链接(此外,在正确实现客户端时,甚至无法触发 Intent ) .

关于android - 如何使用 Google 的 installreferrer 库测试安装引荐来源网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733287/

相关文章:

java - 如何在谷歌服务插件更新后删除警告 "WARNING: API ' variant.getMergeResources( )' is obsolete"?

Android Google Play 服务生命周期

java - 当返回位图的方法失败时,如何处理?

java - 错误:Module 'com.squareup.assertj:assertj-android-recyclerview-v7:1.1.1' depends on one or more Android Libraries but is a jar

java - 在 onOptionsItemSelected 中选择第二个项目会导致应用程序崩溃

android - 有没有办法检测 google play 上 android 应用页面 View 的来源?

android - 无法启动 Activity Java RunTime 异常

适用于所有 SDK 的 Android Google Analytics v2 广播接收器

android - 无法登录手动安装的 APK 的 Google Play 服务

java - 解析 BaseGameActivity