Home » Android » android – Disable ads after in-app purchase

android – Disable ads after in-app purchase

Posted by: admin June 16, 2020 Leave a comment

Questions:

How do I disable Admob ads after in app purchase? What is best way if
the user reinstall the app?

How to&Answers:

Where you would normally initialize the admob, you add a boolean that checks weather or not the person has done the in-app purchase. If the boolean is true(ad removing is purchased) the ads will never appear. If it is false(ad removing is not purchased) it will show ads.

On reinstalling the IAB v3 will get the existing purchases and do action based on it. Permanent purchases like remove ads should not be consumed! If it is the user will have to buy remove ads again if you do not save the boolean or if the user gets a new device. This will cause anger!

When setting up the IAB(IAP) you query the inventory. In there inizialize a boolean(I have called it showAds). In onCreate:(it can be gotten from the Trivial Drive example from google)

 ....(other onCreate stuff. make sure setContentView is called before this:)
 Log.d(TAG, "Starting setup.");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                complain("Problem setting up in-app billing: " + result);
                return;
            }

            // Have we been disposed of in the meantime? If so, quit.
            if (mHelper == null) return;

            mBroadcastReceiver = new IabBroadcastReceiver(Game.this);
            IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
            registerReceiver(mBroadcastReceiver, broadcastFilter);

            // IAB is fully set up. Now, let's get an inventory of stuff we own.
            Log.d(TAG, "Setup successful. Querying inventory.");
            try {
                mHelper.queryInventoryAsync(mGotInventoryListener);

            } catch (Exception e) {
                complain("Error querying inventory. Another async operation in progress.");
            }
        }
    });

    computeAds();

Then outside:(this is not in the Trivial Drive example)

private void computeAds(){
    AdView mAdView = (AdView) findViewById(R.id.adView);
    if(!showAds){
        mAdView.setVisibility(View.GONE);

    }else {
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

computeAds can be called after the purchase which instantly hides the ads. On restart the ad will not even be initialized.

Answer:

This is the best way to remove ads after purchase

 if (!bp.isPurchased("prime")  {
            mAdView = (AdView) findViewById(R.id.adView1);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);


        }else if(bp.isPurchased("prime") ){
            mAdView = (AdView) findViewById(R.id.adView1);

            mAdView.setVisibility(View.GONE);

        }

I am using https://github.com/anjlab/android-inapp-billing-v3 library