Как внедрить API обзора в приложении в приложение для Android с помощью onResume?

В описании говорится о теме, когда запрашивать отзыв:

Код работает нормально, когда я помещаю его в кнопку Click Listener, но Я хочу показать диалоговое окно обзора inApp на onResume() . Когда я вызываю этот метод в 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

Как вызвать этот метод только один раз?

Любая помощь будет оценена

0
0
474
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Просто не вставляйте 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. Но вы можете переместить его на основе вашей бизнес-логики.

Другие вопросы по теме