使用場(chǎng)景
廣告主App開(kāi)發(fā)者也可直接調(diào)用廣告服務(wù)(HUAWEI Ads Kit)的AIDL接口獲取華為設(shè)備上的轉(zhuǎn)化跟蹤參數(shù),這種集成方式不需要集成任何華為SDK。AIDL接口獲取到的轉(zhuǎn)化跟蹤參數(shù)與同一臺(tái)設(shè)備上SDK接口獲取到的轉(zhuǎn)化跟蹤參數(shù)相同。
調(diào)用流程
開(kāi)發(fā)步驟
1.創(chuàng)建接口IPPSChannelInfoService的AIDL文件,放置在com.huawei.android.hms.ppskit包路徑下,如下圖:
2.將以下內(nèi)容復(fù)制到AIDL文件中。
package com.huawei.android.hms.ppskit;
/**重要:請(qǐng)不要修改此AIDL文件的方法順序*/
interface IPPSChannelInfoService{
String getChannelInfo();
}
3.創(chuàng)建一個(gè)類(lèi),實(shí)現(xiàn)Android原生的ServiceConnection接口。
a.實(shí)現(xiàn)ServiceConnection的onServiceConnected方法。
b.調(diào)用Android原生的IPPSChannelInfoService.Stub.asInterface方法獲取IPPSChannelInfoService。
c.調(diào)用getChannelInfo方法獲取轉(zhuǎn)化跟蹤參數(shù)。
private static final String TAG = "InstallReferrerAidlActivity";
private final class InstallReferrerServiceConnection implements ServiceConnection {
private InstallReferrerServiceConnection() {
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i(TAG, "onServiceConnected");
IPPSChannelInfoService service = IPPSChannelInfoService.Stub.asInterface(iBinder);
if (null != service) {
try {
String json = service.getChannelInfo();
parseChannelJson(json);
} catch (RemoteException e) {
Log.e(TAG, "getChannelInfo Exception");
} finally {
getApplicationContext().unbindService(this);
}
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(TAG, "onServiceDisconnected");
}
}
}
4.連接轉(zhuǎn)化跟蹤參數(shù)的AIDL服務(wù)。
private boolean bindService() {
// 創(chuàng)建一個(gè)InstallReferrerServiceConnection實(shí)例
InstallReferrerServiceConnection serviceConnection = new InstallReferrerServiceConnection();
// 創(chuàng)建一個(gè)Intent,Action是“com.huawei.android.hms.CHANNEL_SERVICE”
Intent intent = new Intent("com.huawei.android.hms.CHANNEL_SERVICE");
// 設(shè)置Intent的包名為”com.huawei.hwid”
intent.setPackage("com.huawei.hwid");
// 調(diào)用bindService連接轉(zhuǎn)化跟蹤參數(shù)的AIDL服務(wù)
boolean result = bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);
Log.i(TAG, "bindService result: " + result);
return result;
}
5.解析返回的JSON格式寫(xiě)轉(zhuǎn)化跟蹤參數(shù)。
private ReferrerDetails parseChannelJson(String channelJson) {
Log.i(TAG, "parseChannelJson: " + channelJson);
// 將JSON格式寫(xiě)轉(zhuǎn)化跟蹤參數(shù)
try {
JSONObject jsonObject = new JSONObject(channelJson);
// 跟蹤參數(shù)
String channelInfo = jsonObject.optString("channelInfo");
// 安裝時(shí)間戳
long installTimestamp = jsonObject.optLong("installTimestamp", 0);
// 點(diǎn)擊時(shí)間戳
long clickTimestamp = jsonObject.optLong("clickTimestamp", 0);
ReferrerDetails referrerDetails = new ReferrerDetails(channelInfo, clickTimestamp, installTimestamp);
updateReferrerDetails(referrerDetails);
return referrerDetails;
} catch (JSONException e) {
Log.e(TAG, "");
}
return null;
}
private void updateReferrerDetails(final String installReferrer, final long clickTimestamp,final long installTimestamp) {
Log.i(TAG, "installReferrer: " + installReferrer + ", clickTimestamp: " + clickTimestamp + ", installTimestamp: " + installTimestamp);
}