10

有很多示例如何测试检测安装引用的“默认”方式,但没有示例如何测试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"

不工作,因为我们不知道receiver路径。那么如何测试呢?

4

2 回答 2

7

使用InstallReferrerClient,似乎没有BroadcastReceiverAndroidManifest.xml. 该库只是绑定到系统的安装引荐服务...

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应该有一个)。

raw 的键是Intent Bundle: install_referrer,如果您想尝试模拟它 - 但回调将间接传递结果。referrer_click_timestamp_secondsinstall_begin_timestamp_secondsonInstallReferrerSetupFinished()

该文档还指出:

安装推荐人信息将在 90 天内可用,除非重新安装应用程序,否则不会更改。为避免在您的应用程序中调用不必要的 API,您应该在安装后的第一次执行期间仅调用一次 API。您的应用可以收听系统广播Intent.ACTION_PACKAGE_FIRST_LAUNCH以识别应用的首次执行。


所以这应该是一个intent-filterfor action Intent.ACTION_PACKAGE_FIRST_LAUNCH,它随后将 连接InstallReferrerClientInstallReferrerService。无法使用 触发Intent.ACTION_PACKAGE_FIRST_LAUNCHadb因为它会过滤“受保护的广播操作字符串”,因此它可能仅在从 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 商店的引荐来源链接,然后通过它们安装软件包……否则只会记录默认引荐来源网址(除了正确实施客户端时甚至无法触发意图之外)。

于 2019-09-09T23:21:06.550 回答
1

您可以通过在 Play Market 控制台中设置应用的内部测试版本来测试安装引荐来源网址。之后,使用来自 Play Market URL Builder 的修改后的链接。

于 2019-11-27T15:22:59.117 回答