我正在尝试实现推荐代码系统,而我正在使用Branch.io指标库。我面临的问题是文档不好(不起作用),我无法生成代码
以下是我采取的步骤,包括添加库。
1)抓住jar,添加到我的libs文件夹并将以下内容添加到我的附加内容
compile files('libs/branch-1.5.9.jar')
2)在扩展Application的应用程序类中,我添加了以下内容
if (DEBUG) {
Branch.getAutoTestInstance(this);
} else {
Branch.getAutoInstance(this);
}
3)在我的AndroidManifest.xml中,我添加了以下内容
<meta-data android:name="io.branch.sdk.BranchKey" android:value="@string/bnc_app_key" />
4)在我测试所有内容的Activity中,我在onStart()方法中添加了以下内容
@Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
从上面看,我相信我已经成功创建了一个branch.io会话和一个监听器,如果branchError为null(没有冲突),它将允许我检索数据
虽然仍然在onStart()内部,但我现在尝试生成一个引用代码。所以整个onStart()看起来如下:
@Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
branch.getReferralCode(5, new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
try {
String code = jsonObject.getString("referral_code");
Log.d(TAG, "code: " + code);
} catch (JSONException e) {
Log.e(TAG, "JSONException :: " + e);
e.printStackTrace();
}
}
});
}
5)我添加了onNewIntent覆盖方法
@Override
protected void onNewIntent(Intent intent) {
this.setIntent(intent);
}
我的应用程序未到达onInitFinished侦听器内部,因此我无法检索任何代码。对我所遗漏的任何建议表示赞赏,希望这个帖子能填补文档缺乏的漏洞。
答案 0 :(得分:2)
我将假设以下代码行已正确关闭,并且不会引发语法错误:
branch.initSession(new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this); // parenthesis wasnt here
首先,如果要扩展 Application 类,则需要在Application类的onCreate()
回调中初始化分支:
public void onCreate() {
super.onCreate();
Branch.getInstance(this);
}
根据找到的here分支示例代码,您实际需要致电:
Branch.getInstance();
而不是:
Branch.getInstance(getApplicationContext());
在您的活动中onCreate
回调。完成后,Branch SDK将正确创建。