使用InstallReferrerClient,似乎没有BroadcastReceiver
在AndroidManifest.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_seconds
install_begin_timestamp_seconds
onInstallReferrerSetupFinished()
该文档还指出:
安装推荐人信息将在 90 天内可用,除非重新安装应用程序,否则不会更改。为避免在您的应用程序中调用不必要的 API,您应该在安装后的第一次执行期间仅调用一次 API。您的应用可以收听系统广播Intent.ACTION_PACKAGE_FIRST_LAUNCH
以识别应用的首次执行。
所以这应该是一个intent-filter
for action
Intent.ACTION_PACKAGE_FIRST_LAUNCH
,它随后将 连接InstallReferrerClient
到InstallReferrerService
。无法使用 触发Intent.ACTION_PACKAGE_FIRST_LAUNCH
,adb
因为它会过滤“受保护的广播操作字符串”,因此它可能仅在从 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 商店的引荐来源链接,然后通过它们安装软件包……否则只会记录默认引荐来源网址(除了正确实施客户端时甚至无法触发意图之外)。