Remove ads when user makes in-app-purchase

895 views Asked by At

I am trying to implement the in-app-billing API and make my app without ads when the user buy "Remove ads" in-app-purchase. The problem is I am not sure where to do that. I have a FAB button and adview inside a linierlayout like this: enter image description here

and as soon as the user buy the it I will set the adview visibility to GONE so the FAB button goes down.

This is my main activity code:

public class MainActivity extends AppCompatActivity {
private static final String TAG =  MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private RecyclerView mRecyclerView;
private ArrayList<String> shoppingListItems;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private TextView mEmptyTextView;
private ShoppingListAdapter adapter;
private ActionButton actionButton;
private MaterialDialog addItemdialog = null;
private IabHelper mHelper;
private String SKU_REMOVE_ADDS = "remove_adds_sku";
private boolean mIsRemoveAdds = false;
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //load ads
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
//set up billing
    String publicKey = "PUBLIC_KEY";

    mHelper = new IabHelper(this,publicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if(!result.isSuccess()){
                //error
                Log.d(TAG,"Proglem setting up in-app Billing: " + result);
            }

            //Horay, IAB is fully set up!
            Log.d(TAG,"Horay, IAB is fully set up!");
        }
    });
    queryPurchasedItems();
}
 @Override
protected void onResume() {
    super.onResume();
    isListEmpty();
    queryPurchasedItems();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}
private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                // handle error here
            }
            else{
                // does the user have the premium upgrade?
                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
                // update UI accordingly
            }
        }
    };
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(id == R.id.action_remove_adds){
        mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,"");
    }
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        //setContentView(R.layout.activity_main_adds);
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
        //setContentView(R.layout.activity_main_adds);
    }
}
1

There are 1 answers

4
Hadi Satrio On BEST ANSWER

Yep, your solution is suitable for that kind of scenario. In fact, I've used a similar approach for my app and it's working fine for multiple IAP transactions.

One suggestion, though. You should persists mIsRemoveAdds in your SharedPreferences just in case. With that done, you could retrieve the value from SharedPreferences when you detect that the device is not connected to the internet (airplane mode, or perhaps just bad reception).

Other than that, your code is good.