天天看点

获取GooglePlay下载来源广告第二种方式

第一种方式以google play 发送广播方式

第二种就是以下方式:

dependencies {
    ...
    compile 'com.android.installreferrer:installreferrer:1.0'
}      
final InstallReferrerClient mReferrerClient = InstallReferrerClient.newBuilder(this).build();
mReferrerClient.startConnection(new InstallReferrerStateListener() {
    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        Log.e("e", responseCode+"responseCode");
        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                // Connection established 已建立连接
                ReferrerDetails response = null;
                try {
                    response = mReferrerClient.getInstallReferrer();
                    response.getInstallReferrer();
                    response.getReferrerClickTimestampSeconds();
                    response.getInstallBeginTimestampSeconds();
                    sendlog( response.getInstallReferrer());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
              break;
            case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                // API not available on the current Play Store app   已安装的Play Store应用程序不支持的Referrer API              
                break;
            case InstallReferrerClient.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.
    }
});