Android 解析Google Play回传参数Referre

2020-02-17  本文已影响0人  Typin

Play Install Referrer 库见:
https://developer.android.com/google/play/installreferrer/library

Google Play 网址构建工具见:
https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

步骤1:

    dependencies {
        compile 'com.android.installreferrer:installreferrer:1.0'
    }
    

步骤2:

 InstallReferrerClient referrerClient
    ...
    referrerClient = InstallReferrerClient.newBuilder(this).build();
    referrerClient.startConnection(new InstallReferrerStateListener() {
        @Override
        public void onInstallReferrerSetupFinished(int responseCode) {
            switch (responseCode) {
                case InstallReferrerResponse.OK:
                    // Connection established
ReferrerDetails response = referrerClient.getInstallReferrer();
    response.getInstallReferrer();
    response.getReferrerClickTimestampSeconds();
    response.getInstallBeginTimestampSeconds();
                    break;
                case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    // API not available on the current Play Store app
                    break;
                case InstallReferrerResponse.SERVICE_UNAVAILABLE:
                    // Connection could not be established
                    break;
            }
        }

        @Override
        public void onInstallReferrerServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });

response.getInstallReferrer()是返回的referrer参数,解析方式如下:

private static Map<String, String> parse(String data) {
        if (TextUtils.isEmpty(data)) {
            return Collections.EMPTY_MAP;
        }
        try {
            String[] kvs = data.split("&");
            HashMap<String, String> map = new HashMap<>();
            for (int i = 0; i < kvs.length; i++) {
                String temp = kvs[i];
                if (TextUtils.isEmpty(temp))
                    continue;
                String[] pair = temp.split("=");
                if (pair != null && pair.length == 2) {
                    map.put(pair[0], pair[1]);
                }
            }
            return map;
        } catch (Exception e) {
            return Collections.EMPTY_MAP;
        }
    }
上一篇下一篇

猜你喜欢

热点阅读