简体   繁体   中英

Remove Ads when In-App Billing is successful

In my class where I implemented the In-App Billing I have this onIabPurchaseFinished() method:

@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {

    if (!verifyDeveloperPayload(info)) {
        Toast.makeText(this, R.string.error_purchasing, Toast.LENGTH_LONG).show();
    }

    Toast.makeText(this, R.string.premium_bought, Toast.LENGTH_LONG).show();

    if (info.getSku().equals("chords_premium")) {
        // bought the premium upgrade!
        isPremium = true;

        /** salva isPremium tra SharedPreferences */
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("isPremium", isPremium);
        editor.apply();
    }
}

As you can see when the purchase is successful isPremium is set to true and saved in SharedPreferences

What I need to do now is not show AdMob ads when the purchase is successful.

This is an example of how I did it in a class I have implemented Ads in:

// I get the boolean from SharedPreferences
SharedPreferences PremiumPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean isPremium = PremiumPref.getBoolean("isPremium", false);

and the I manage the ads like this with the if(!isPremium) :

if (!isPremium) {
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-6723047396589178/2654753246");

        AdView listBanner = (AdView) findViewById(R.id.chords_list_banner);
        AdRequest adRequest = new AdRequest.Builder().build();
        listBanner.loadAd(adRequest);

        /** carica Ad a tutto schermo */
        chordsListAd = new InterstitialAd(this);
        chordsListAd.setAdUnitId("ca-app-pub-6723047396589178/7447672046");
        requestNewInterstitial();


        chordsListAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });
    }

The problem is that when I test it, and buy the Premium upgrade, Ads are still showing!

As you can see it's not only Banners I need to remove, but also Interstitial Ads

What am I doing wrong? how can I fix it?

EDIT:

this is the progress I have made:

@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {

    if (!verifyDeveloperPayload(info)) {
        Toast.makeText(this, R.string.error_purchasing, Toast.LENGTH_LONG).show();
    }

    Toast.makeText(this, R.string.premium_bought, Toast.LENGTH_LONG).show();

    if (info.getSku().equals("chords_premium")) {
        // bought the premium upgrade!
        isPremium = true;

        /** salva isPremium tra SharedPreferences */
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("isPremium", isPremium);
        editor.apply();

        removeAds();
    }
}

public void removeAds() {
    AdView listBanner = (AdView) findViewById(R.id.chords_list_banner);
    listBanner.setVisibility(View.GONE);

    AdView chordBanner = (AdView) findViewById(R.id.chord_banner);
    chordBanner.setVisibility(View.GONE);

    AdView searchBanner = (AdView) findViewById(R.id.search_ad);
    searchBanner.setVisibility(View.GONE);
}

as you can see I've added the removeAds() method as suggested in the answer. But how do I remove the Interstitials in this method?

You should call a method in your onIabPurchaseFinished , something like hideAds , which would hide your current shown AdView with a simple snippet like this:

AdView listBanner = (AdView) findViewById(R.id.chords_list_banner);
listBanner.setVisibility(View.GONE);

EDIT: Also later on the In-app-billing setting should be checked when the app starts whether the premium is already bought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM