1.添加Play Core SDK
在.grade文件dependencies中添加如下代码:
dependencies {
//gp应用内评价
implementation 'com.google.android.play:core:1.10.0'
}
2. 请求 ReviewInfo 对象
在第一个Activity脚本中初始化应用内评价
请遵循有关何时请求应用内评价的指南,以确定在应用的用户流的哪些阶段适合提示用户进行评价(例如,当用户在游戏中完成某个关卡时)。当您的应用达到其中一个阶段时,请使用
[ReviewManager](https://developer.android.google.cn/reference/com/google/android/play/core/review/ReviewManager)
实例创建请求任务。如果请求成功,该 API 将返回启动应用内评价流程所需的 [ReviewInfo](https://developer.android.google.cn/reference/com/google/android/play/core/review/ReviewInfo)
对象。
private ReviewManager manager;
private ReviewInfo reviewInfo;
@Override protected void onCreate(Bundle savedInstanceState)
{
manager = ReviewManagerFactory.create(mContext);
//manager = new FakeReviewManager(mContext); //模拟调用api是否正常,需要用上面代码发布测试渠道测试
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
// We can get the ReviewInfo object
reviewInfo = task.getResult();
Log.i(TAG, "initHwSDK gp: "+ Thread.currentThread().getId());
} else {
// There was some problem, log or handle the error code.
//@ReviewErrorCode int reviewErrorCode = ((TaskException) task.getException()).getErrorCode();
Log.i(TAG, "initHwSDK gp error: ");
}
});
}
3.启动应用内评价流程
在游戏逻辑中调用以下java代码接口,使用 [ReviewInfo](https://developer.android.google.cn/reference/com/google/android/play/core/review/ReviewInfo)
实例启动应用内评价流程。等到用户完成应用内评价流程后,再继续执行应用的正常用户流(例如进入下一关)。
public void showComment(){
Log.i(TAG, "showComment: ");
if(manager != null && reviewInfo != null){
Task<Void> flow = manager.launchReviewFlow(mContext, reviewInfo);
flow.addOnCompleteListener(task -> {
// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown. Thus, no
// matter the result, we continue our app flow.
Log.i(TAG, "showComment: success");
});
}
}
网友评论