有很多示例如何测试检测安装引用的“默认”方式,但没有示例如何测试 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"
不起作用,因为我们不知道接收者
路径。那么如何测试呢?
最佳答案
与 InstallReferrerClient ,AndroidManifest.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_referrer
、referrer_click_timestamp_seconds
和install_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_LAUNCH
的intent-filter
,它随后连接InstallReferrerClient
到InstallReferrerService
。无法使用 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/