Get referrer after installing app from Android Mar

2019-01-01 11:10发布

问题:

I am trying to register a Broadcast Receiver that catches \"com.android.vending.INSTALL_REFERRER\" intents launched by Android after an app is installed from the Market.

I am following the details here: http://code.google.com/mobile/analytics/docs/android/#referrals

However, I cannot use Google Analytics so I have created my own solution. I have added the following to my manifest file:

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

and created a basic BroadcastReceiver class:

public class Receiver extends BroadcastReceiver {

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

        Bundle extras = intent.getExtras();
        String referrerString = extras.getString(\"referrer\");

        Log.w(\"TEST\", \"Referrer is: \" + referrerString);
    }
}

However, when the app is installed the receiver doesn\'t seem to catch the Intent (if the Intent is even broadcast?) and I get no logging output.

Am I going wrong somewhere or is the Market no longer launching these Intents when an app is installed?

回答1:

I would try to help who, like me, fails to make install_referrer work and who don\'t find ANY useful information about these features.

Notes:

  1. The intent com.android.vending.INSTALL_REFERRER will be caught during the install process, not when the application starts for the first time.
  2. The referrer ...extras.getString(\"referrer\").. is fixed but the contents can be any string value that respect the http get syntax ...referrer=thatsthevalue&thisisnot=xxx

The above code is okay, just some explanations to complete the info:

  1. Android Manifest. The <receiver> tags must be inside the <application> tags.
  2. The correct url to link the market is not the results of the famous google forms in sdk

but this one

http://market.android.com/details?id=your.application.package.name&referrer=my_referrer_finally_works_fine

Obviously, you need to follow the link from the mobile device and the only way for a complete test is to publish a test application in the market.

And a final and personal note.

I don\'t understand why those info are omitted completely and i hope that Google will act for detailing it.



回答2:

This might be a little late, but you CAN test the install referrer without using Google Play, just use ADB :)

Run this from adb.exe

adb shell

am broadcast -a com.android.vending.INSTALL_REFERRER -n your.package/path.to.your.BroadcastReceiver --es \"referrer\" \"test_referrer=test\"

If you have logging setup in your BroadcastReceiver, you will see it popup in LogCat.

Hope this helps!



回答3:

Okay so I found the reason why the Intent wasn\'t being launched. Apparently you MUST use the same parameter names as outlined here: http://code.google.com/mobile/analytics/docs/android/#referrals

You cant use your own parameter names as I was doing :S



回答4:

I think these answers must have been written pre-Android 3.1 - because things have changed in one import way.

The system now marks app as dormant when they are installed - they won\'t receive INSTALL_REFERRER or any other broadcast until the user explicitly activates the app by running it (or placing widget).



回答5:

I agree that Google\'s documentation isn\'t the best. However, I\'ve only been able to get the intent to fire by actually uploading the app to the Market Place and then downloading/installing it. The intent does launch immediately after the download/instal - the user does not have to start the app. I\'m using this to start a background service as well.



回答6:

Please notice that this is not the first start intent but only a android market related intent which is sent my the google android market. If you install the app through a different resource than the android market it will not fire.

Use the link which you can build there: http://code.google.com/mobile/analytics/docs/android/#android-market-tracking get the referrer from the intent and take it apart to get the different parameters

referrer = intent.getStringExtra(\"referrer\");
Map<String, String> params = Toolbox.getQueryMap(referrer);

P.S. You don\'t need to read to read the deviceid/IMEI to do this, as some apps do. You shouldn\'t want to spy out your users.



回答7:

Actually there could be any links like market://... or http://market... either work fine. Also it doesn\'t matter what kind of parameters will be in the referrer field. It works fine with any text in there.

The main issue that this event type \"com.android.vending.INSTALL_REFERRER\" doesn\'t send by broadcast. This event goes ONLY into the just installed application.

UPD: And there only one way to test it - deploy your App into the Market and then install it on the phone.



回答8:

Use Google Play Referrer API (from 20 Nov 2017) very easily and securely as i answered here



回答9:

1) Broadcast receiver

public class InstallReferrerReceiver extends BroadcastReceiver {

    String referrer = \"\";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null) {
            if (intent.getAction().equals(\"com.android.vending.INSTALL_REFERRER\")) {

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

                    Log.e(\"Receiver Referral\", \"===>\" + referrer);                  
                    PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER, referrer);

                    /*
                    If you want split username and code use below code...
                    for ex referrer=\"username12345678890\"
                    if(referrer!=null)
                    {
                        String[] referrerParts = referrer.split(\"(?<=\\\\D)(?=\\\\d)\");
                        String strName = referrerParts[0];
                        String strCode = referrerParts[1];

                        Log.e(\"Receiver Referral Code\", \"===>\" + strName);
                        Log.e(\"Receiver Referral Name\", \"===>\" + strCode);

                        PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER_CODE, strName);
                        PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER_NAME, strCode);
                    }*/
                }
            }
        }
    }
}

2) Define a receiver in your app manifest.in which com.android.vending.INSTALL_REFERRER 

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

3) Test your app configuration running this command

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n package_name/broadcast_receiver_path_with_packagename --es referrer \"username1234567890\"