使用InstallReferrerClient时,似乎没有在AndroidManifest.xml
中注册任何BroadcastReceiver
。该库只是绑定到系统的安装引荐服务...
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_referrer
,
referrer_click_timestamp_seconds
和
install_begin_timestamp_seconds
,如果您想尝试模拟它 - 但是
onInstallReferrerSetupFinished()
回调将间接地提供结果。
文档还指出:
安装引荐信息将可用90天,并且除非重新安装应用程序,否则不会更改。为避免您的应用程序中不必要的 API 调用,您应该在安装后仅在第一次执行期间调用 API。您的应用程序可以监听系统广播 Intent.ACTION_PACKAGE_FIRST_LAUNCH
来识别应用程序的第一次执行。
这应该是一个针对
action
Intent.ACTION_PACKAGE_FIRST_LAUNCH
的
intent-filter
,随后将
InstallReferrerClient
连接到
InstallReferrerService
。无法使用
adb
触发
Intent.ACTION_PACKAGE_FIRST_LAUNCH
,因为它过滤了“受保护的广播操作字符串”,因此只有在从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 Store并通过它们安装包...否则只有默认的引荐者将被记录(除非在正确实现客户端时甚至无法触发意图)。