如何使用 onResume 将应用内评论 API 实施到 android 应用中?

how to implement in-app review API into android app with onResume?

description 关于何时要求评论的主题说明:

当我将代码放入按钮点击监听器时,代码工作正常,但是 我想在 onResume() 上显示 inApp 评论对话框。当我在 onResume 上调用此方法时,它会一次又一次地调用并且永不停止。我通过将 Log 放入 addOnCompleteListener 方法中找到了它。

   @Override
protected void onResume() {
    Log.d("inAppReview", "reviewed");

    if (reviewInfo != null) {
        ReviewManager manager = ReviewManagerFactory.create(this);
        Task<ReviewInfo> request = manager.requestReviewFlow();
        request.addOnCompleteListener(task -> {
            try {
                if (task.isSuccessful()) {
                    // We can get the ReviewInfo object
                    ReviewInfo reviewInfo = task.getResult();
                    Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                    flow.addOnCompleteListener(task2 ->
                    {
                        // 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.
                        //  utility.logMessageAsync(activity, "In-app review returned.");
                        Log.d("inAppReview", "review successful");

                    });

                } else {
                    // There was some problem, continue regardless of the result.
                    // goToAppPage(activity);
                }
            } catch (Exception ex) {
                Log.d("inAppReview", "exception");
                // utility.logExceptionAsync(activity, "Exception from openReview():", ex);
            }
        });
        super.onResume();
    } else {
        Log.d("inAppReview", "Error While Reviewing");
    }
    super.onResume();}

下面附上我的 Logcat

的照片

如何只调用一次该方法?

如有任何帮助,我们将不胜感激

干脆不要放在 onResume 中。这是一个不好的做法

boolean 检查应用程序是否经过如下审核:

 boolean isAppReviewed = false;

 @Override
 protected void onResume() {
 if(isAppReviewed) {
     super.onResume();
     return;
 }
 Log.d("inAppReview", "reviewed");

 if (reviewInfo != null) {
    ReviewManager manager = ReviewManagerFactory.create(this);
    Task<ReviewInfo> request = manager.requestReviewFlow();
    request.addOnCompleteListener(task -> {
        try {
            if (task.isSuccessful()) {
                // We can get the ReviewInfo object
                ReviewInfo reviewInfo = task.getResult();
                Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                flow.addOnCompleteListener(task2 ->
                {
                    // 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.
                    //  utility.logMessageAsync(activity, "In-app review returned.");
                    Log.d("inAppReview", "review successful");
                    isAppReviewed = true;
                });

            } else {
                // There was some problem, continue regardless of the result.
                // goToAppPage(activity);
            }
        } catch (Exception ex) {
            Log.d("inAppReview", "exception");
            // utility.logExceptionAsync(activity, "Exception from openReview():", ex);
        }
    });
    super.onResume();
} else {
    Log.d("inAppReview", "Error While Reviewing");
}
super.onResume();
}

只要适合停止显示评论,您就可以将 isAppReviewed 更新为 true,在我的示例中,我在 flow.addOnCompleteListener 中执行此操作。但是您可以根据您的业务逻辑移动它。