INSTALL_REFERRER 应用程序启动前未收到

INSTALL_REFERRER not received before application launched

我想在我自己的接收器上捕获 INSTALL_REFERRER 意图。 我实现了接收器

public class InstallReferrerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
           Log.d("Broadcast", "RECEIVED!");
        }
}

并在清单中添加

<receiver
    android:name=".receiver.InstallReferrerReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER"/>
    </intent-filter>
</receiver>

当我想测试接收器时,我安装我的应用程序(不启动)并通过 adb 发送广播

am broadcast -a com.android.vending.INSTALL_REFERRER --es "referrer" "utm_medium=partner&utm_campaign=partner_name"

但是看不到任何日志。午餐后,它正常工作并接收意图。

来自"Testing Google Play Campaign Measurement"

To broadcast the INSTALL_REFERRER intent to your application:

  1. Verify that your application is not currently running.
  2. Open a terminal and run this command: ...

但是在我第一次启动应用程序之前,我的接收器没有收到意图。这是正确的行为吗?如果我使用引荐来源参数从市场安装应用程序,当我收到此意图时?

谢谢

On Android 3.1+ 应用程序的 BroadcastReceiver(或任何其他组件)将不会被触发,直到用户至少启动该应用程序一次。在那之前它处于 "stopped" 状态

is an intended behaviour 并防止一些安全风险。

您的 adb 命令不完整。试试这个:

./adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n <your package>/<your package>.receiver.InstallReferrerReceiver --es "referrer" "utm_medium%3Dpartner%26utm_campaign%3Dpartner_name"

注意:引荐来源网址应 url 编码。

发送 broadcastintent 时必须设置标志 Intent.FLAG_INCLUDE_STOPPED_PACKAGES

Bundle extras = new Bundle();
extras.putString("referrer", referrer);
Intent intent = new Intent("com.android.vending.INSTALL_REFERRER");
intent.putExtras(extras);
intent.setPackage(packageChanged);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);