4.集成轉(zhuǎn)化跟蹤參數(shù)開(kāi)放能力(SDK方式)
SDK方式是指通過(guò)HUAWEI Ads Kit提供的SDK接入開(kāi)放能力,接入方式簡(jiǎn)單,推薦使用此方式。
1、集成SDK
步驟1 配置HUAWEI Ads SDK的Maven倉(cāng)地址。
打開(kāi)Android Studio項(xiàng)目級(jí)build.gradle文件,在allprojects->repositories里面配置SDK的Maven倉(cāng)地址。
allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
步驟2 配置依賴
打開(kāi)子工程app下的build.gradle文件,在dependencies新增SDK的依賴,并將{version}替換為最新HUAWEI Ads SDK版本號(hào),參見(jiàn)版本更新說(shuō)明。
dependencies {
implementation 'com.huawei.hms:ads-installreferrer:{version}'
}
步驟3 同步修改的文件
步驟4 配置混淆腳本(必選)
app/proguard-rules.pro
-keep class com.huawei.hms.ads.**{*;}
-keep interface com.huawei.hms.ads.**{*;}
2、創(chuàng)建轉(zhuǎn)化跟蹤參數(shù)狀態(tài)監(jiān)聽(tīng)器
實(shí)現(xiàn)InstallReferrerStateListener監(jiān)聽(tīng)器接口的兩個(gè)方法onInstallReferrerSetupFinished和onInstallReferrerServiceDisconnected,代碼示例如下:
private InstallReferrerStateListener installReferrerStateListener = new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
Log.i(TAG, "connect ads kit ok");
get();
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
Log.i(TAG, "FEATURE_NOT_SUPPORTED");
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
Log.i(TAG, "SERVICE_UNAVAILABLE");
break;
default:
Log.i(TAG, "responseCode: " + responseCode);
break;
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
Log.i(TAG, "onInstallReferrerServiceDisconnected");
}
};
在連接成功的回調(diào)中調(diào)用getInstallReferrer方法獲取轉(zhuǎn)化跟蹤參數(shù)。
InstallReferrerSdkUtil.java
private void get() {
if (null != mReferrerClient) {
try {
ReferrerDetails referrerDetails = mReferrerClient.getInstallReferrer();
if (null != referrerDetails && null != mCallback) {
mCallback.onSuccuss(referrerDetails.getInstallReferrer(),
referrerDetails.getReferrerClickTimestampMillisecond(),
referrerDetails.getInstallBeginTimestampMillisecond());
}
} catch (RemoteException e) {
Log.i(TAG, "getInstallReferrer RemoteException: " + e.getMessage());
} catch (IOException e) {
Log.i(TAG, "getInstallReferrer IOException: " + e.getMessage());
} finally {
disconnect();
}
}
}
3、創(chuàng)建InstallReferrerClient實(shí)例
通過(guò)builder配置和創(chuàng)建一個(gè)InstallReferrerClient實(shí)例。
開(kāi)發(fā)步驟:
步驟1 調(diào)用InstallReferrerClient.newBuilder創(chuàng)建一個(gè)Builder實(shí)例。
步驟2 調(diào)用build創(chuàng)建InstallReferrerClient實(shí)例。
InstallReferrerSdkUtil.java
mReferrerClient=InstallReferrerClient.newBuilder(mContext).build();
4、連接轉(zhuǎn)化跟蹤參數(shù)服務(wù)
建立與轉(zhuǎn)化跟蹤參數(shù)服務(wù)的連接,不要在主線程中調(diào)用該方法。
開(kāi)發(fā)步驟:
步驟1 設(shè)置startConnection的監(jiān)聽(tīng)器為已創(chuàng)建的InstallReferrerStateListener監(jiān)聽(tīng)器。
步驟2 在子線程中調(diào)用startConnection方法連接服務(wù)。
InstallReferrerSdkUtil.java
mReferrerClient.startConnection(installReferrerStateListener);
5.集成轉(zhuǎn)化跟蹤參數(shù)開(kāi)放能力(AIDL方式)
1、新增AIDL接口文件
在工程中新增一個(gè)aidl接口文件,包名為com.huawei.android.hms.ppskit,文件名稱為IPPSChannelInfoService.aidl,拷貝下面內(nèi)容到文件中。
app/src/main/aidl/com/huawei/android/hms/ppskit/IPPSChannelInfoService.aidl
//IPPSRemoteService.aidl
package com.huawei.android.hms.ppskit;
/*
*Important:Do not modify the method sequence of the AIDL file.
*/
interface IPPSChannelInfoService{
String getChannelInfo();
}
2、創(chuàng)建一個(gè)類,實(shí)現(xiàn)ServiceConnection接口。
開(kāi)發(fā)步驟:
步驟1 實(shí)現(xiàn)ServiceConnection的onServiceConnected方法。
步驟2 調(diào)用IPPSChannelInfoService.Stub.asInterface方法獲取IPPSChannelInfoService。
步驟3 調(diào)用getChannelInfo方法獲取轉(zhuǎn)化跟蹤參數(shù)。
InstallReferrerAidlUtil.java
private final class InstallReferrerServiceConnection implements ServiceConnection {
private InstallReferrerServiceConnection() {
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i(TAG, "onServiceConnected");
mService = IPPSChannelInfoService.Stub.asInterface(iBinder);
if (null != mService) {
try {
String channelJson = mService.getChannelInfo();
Log.i(TAG, "channelJson: " + channelJson);
JSONObject jsonObject = new JSONObject(channelJson);
String installReferrer = jsonObject.optString("channelInfo");
long clickTimestamp = jsonObject.optLong("clickTimestamp", 0);
long installTimestamp = jsonObject.optLong("installTimestamp", 0);
if (null != mCallback) {
mCallback.onSuccuss(installReferrer, clickTimestamp, installTimestamp);
} else {
mCallback.onFail("install referrer is empty");
}
} catch (RemoteException e) {
Log.e(TAG, "getChannelInfo RemoteException");
mCallback.onFail(e.getMessage());
} catch (Exception e) {
Log.e(TAG, "getChannelInfo Excepition");
mCallback.onFail(e.getMessage());
} finally {
unbindService();
}
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(TAG, "onServiceDisconnected");
mService = null;
}
}
3、連接轉(zhuǎn)化跟蹤參數(shù)的AIDL服務(wù)
開(kāi)發(fā)步驟:
步驟1 創(chuàng)建一個(gè)InstallReferrerServiceConnection實(shí)例。
步驟2 創(chuàng)建一個(gè)Intent,Action是"com.huawei.android.hms.CHANNEL_SERVICE"。
步驟3 設(shè)置Intent的包名為"com.huawei.hwid"。
步驟4 調(diào)用bindService連接轉(zhuǎn)化跟蹤參數(shù)的AIDL服務(wù)。
InstallReferrerAidlUtil.java
private boolean bindService() {
Log.i(TAG, "bindService");
if (null == mContext) {
Log.e(TAG, "context is null");
return false;
}
mServiceConnection = new InstallReferrerServiceConnection();
Intent intent = new Intent(Constants.SERVICE_ACTION);
intent.setPackage(Constants.SERVICE_PACKAGE_NAME);
boolean result = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG, "bindService result: " + result);
return result;
}
4、斷開(kāi)轉(zhuǎn)化跟蹤參數(shù)的AIDL服務(wù)
使用完AIDL接口后,應(yīng)該主動(dòng)斷開(kāi)服務(wù)連接。
InstallReferrerAidlUtil.java
private void unbindService() {
if (null != mServiceConnection) {
unbindService(mServiceConnection);
}
}