在应用内结算成功后删除广告

时间:2016-09-10 13:28:40

标签: java android in-app-billing ads

在我实施应用内结算的课程中,我有onIabPurchaseFinished()方法:

@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();
    }
}

您可以看到购买成功的时间isPremium设置为true并保存在SharedPreferences

购买成功后,我现在需要做的就是不显示AdMob广告。

这是我在我实施过广告的课程中如何做到这一点的一个例子:

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

我使用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();
            }
        });
    }

问题是,当我测试它并购买Premium升级时,广告仍在显示!

正如您所看到的,我不仅要删除横幅广告,还要插入非页内广告

我做错了什么?我该如何解决?

修改

这是我取得的进步:

@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);
}

正如您所看到的,我已按照答案中的建议添加了removeAds()方法。但如何删除此方法中的插页式广告?

1 个答案:

答案 0 :(得分:0)

您应该在onIabPurchaseFinished中调用一种方法,例如hideAds,这会隐藏您当前显示的AdView,其中包含以下简单代码:

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

编辑: 此后,应用程序启动时应检查应用内结算设置是否已购买保费。