android:从Google Play跟踪安装

时间:2014-10-30 08:12:01

标签: android

当用户点击我的应用中将重定向到Play商店的链接时,我一直在尝试了解应用是否已安装。我没有使用Google的任何广告系列或工具。

基本上,在我的活动中,点击后,

Intent inte = new Intent(Intent.ACTION_VIEW, Uri.parse(
                "market://details?id=com.example.example&referrer=utm_source%3Dmy_referrer"));
        startActivity(inte);

我在应用程序标签的清单中有一个广播接收器:

 <!-- Install Referrer -->
        <receiver
            android:name="com.ex.ex.ReferrerReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" >
                </action>
            </intent-filter>
        </receiver>

广播接收器代码:

package com.ex.ex;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class ReferrerReceiver extends BroadcastReceiver {
    private final String TAG = "my_package";

    private String referrer = "";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "Install received");

        Bundle extras = intent.getExtras();
           if(extras != null){
              referrer = extras.getString("referrer");

           }

           Log.w(TAG,"Referrer is: "+ referrer);

    }

}

但是,这个广播接收器永远不会被触发。

这不是安装跟踪器应该如何工作的吗?

1 个答案:

答案 0 :(得分:0)

我在我的活动中这样做了:

private BroadcastReceiver mInstallReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Your code
        // Package name is in intent.getData() or intent.getDataString()
        // intent.getDataString() == "package:com.some.packagename"
    }
};

private void registerReceiver() {
    IntentFilter installFilter = new IntentFilter();
    installFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    installFilter.addDataScheme("package");
    registerReceiver(mInstallReceiver, installFilter);
}

@Override
protected void onStart() {
    super.onStart();
    registerReceiver();
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(mInstallReceiver);
}