How to display AdMob's rewarded ad in webview via JavaScript in Android?

71 views Asked by At

I'm planning to display a admob rewarded ad by a button click in a webview via javascript. The ad runs but a black screen is displayed. That is, the listener onAdLoaded() and onAdShowedFullScreenContent() are both executed, but it does not display an ad and only displays a black page. In addition, I also use test tokens and when I put the ad display command in the onAdLoaded() listener, the ad is displayed well. Only when I call with javascript, I have the black page problem.

javascript code:

function kurosh(){
            JSInterface.getData("hashcode");
        }

android code:

public class JSInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        JSInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void getData(String hashCode) {
            Hashcode = hashCode;
            showRewardAd();
        }
    }
wv.addJavascriptInterface(new JSInterface(c), "JSInterface");
private void initializeMobileAdsSdk() {
        /*if (isMobileAdsInitializeCalled.getAndSet(true)) {
            return;
        }*/

        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(c, "ca-app-pub-3940256099942544~3347511713");
        /*List<String> testDeviceIds = Arrays.asList("EE3D38ABAA050D0967ABE3F07EDFA862");
        RequestConfiguration configuration =
                new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
        MobileAds.setRequestConfiguration(configuration);*/

        // Load an ad.
        loadRewardedAd();
    }

private void loadRewardedAd() {
        if (rewardedAd == null) {
            isLoading = true;
            AdRequest adRequest = new AdRequest.Builder().build();
            RewardedAd.load(
                    this,
                    "ca-app-pub-3940256099942544/5224354917",
                    adRequest,
                    new RewardedAdLoadCallback() {
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            // Handle the error.
                            Log.d("ERROR LOG REWARD", loadAdError.getMessage());
                            rewardedAd = null;
                            comment.this.isLoading = false;
                            Toast.makeText(comment.this, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
                            comment.this.rewardedAd = rewardedAd;
                            Log.d("ERROR LOG REWARD", "onAdLoaded");
                            comment.this.isLoading = false;
                            Toast.makeText(comment.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
                            //showRewardAd();
                        }
                    });
        }
    }

public void showRewardAd(){
        runOnUiThread(new Runnable() {
            public void run() {
                showRewardedVideo();
            }

        });
    }
    public void showRewardedVideo() {
        if (rewardedAd == null) {
            Log.d("TAG", "The rewarded ad wasn't ready yet.");
            return;
        }
        rewardedAd.setFullScreenContentCallback(
                new FullScreenContentCallback() {
                    @Override
                    public void onAdShowedFullScreenContent() {
                        // Called when ad is shown.
                        Log.d("ERROR LOG REWARD", "onAdShowedFullScreenContent");
                        Toast.makeText(comment.this, "onAdShowedFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                        // Called when ad fails to show.
                        Log.d("ERROR LOG REWARD", "onAdFailedToShowFullScreenContent");
                        // Don't forget to set the ad reference to null so you
                        // don't show the ad a second time.
                        rewardedAd = null;
                        Toast.makeText(comment.this, "onAdFailedToShowFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                    }

                    @Override
                    public void onAdDismissedFullScreenContent() {
                        // Called when ad is dismissed.
                        // Don't forget to set the ad reference to null so you
                        // don't show the ad a second time.
                        rewardedAd = null;
                        Log.d("ERROR LOG REWARD", "onAdDismissedFullScreenContent");
                        Toast.makeText(comment.this, "onAdDismissedFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                        //if (googleMobileAdsConsentManager.canRequestAds()) {
                            // Preload the next rewarded ad.
                        comment.this.loadRewardedAd();
                        comment.this.adShowFinished();

                        //}
                    }
                });
        Activity activityContext = comment.this;

        rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                // Handle the reward.
                Log.d("TAG", "The user earned the reward.");
                adstatus = true;
            }
        });
    }

enter image description here

whats problem?

I tried different methods and changed the parameters, but the result was still the same black screen. Of course, it was shown once, but it was not shown again

0

There are 0 answers