In-App-Billing Remove Ads Works after payment but after destroying the app not

174 views Asked by At

I am trying to use the in-app-billing API for the first time. I have tested the app with a different account and the payment went through and the ads were removed from the app. However, when I destroyed the app and they launching it again the ads were present again. I dont understand what I am doing wrong. Please does anybody has a suggestion?

This is my code in main acticity:

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 AdView mAdView;
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;
            mAdView.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEmptyTextView = (TextView)findViewById(R.id.list_empty);
    mEmptyTextView.setVisibility(View.INVISIBLE);
    mSharedPreferences = getPreferences(MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();



    queryPurchasedItems();
    //load ads
    if(!mIsRemoveAdds) {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }else{
        mAdView.setVisibility(View.GONE);
    }
String publicKey = s1+s2+s3+s4+s5;

    mHelper = new IabHelper(this,publicKey);
    if(mHelper != null) {
        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!");
            }
        });
    }

}
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
                Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
            }
            else{
                // does the user have the premium upgrade?
                mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
                if(!mIsRemoveAdds){
                    Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                }
                // update UI accordingly

                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }
        }
    };
}
@Override
protected void onResume() {
    super.onResume();

    queryPurchasedItems();
    //Toast.makeText(MainActivity.this,"On Resume",Toast.LENGTH_LONG).show();
    //read isRemoveAdds from shared preferences
    //mIsRemoveAdds = mSharedPreferences.getBoolean(Constants.IS_REMOVE_ADDS,false);
    if(!mIsRemoveAdds) {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }else{
        mAdView.setVisibility(View.GONE);
    }
    isListEmpty();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
    mAdView.destroy();
}

 @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

2
Joseph Roque On BEST ANSWER

You need to move your if statement

if(!mIsRemoveAdds) {
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}else{
    mAdView.setVisibility(View.GONE);
}

to onQueryInventoryFinished. This is because you're displaying ads before the app has a chance to check if the user has purchased the upgrade, and then after the check is complete, you never hide the ads again.

So, remove the above if statement from your onCreate and onResume method and format your onQueryInventoryFinished method like so:

    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            // handle error here
            Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
        }
        else{
            // does the user have the premium upgrade?
            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds){
                Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();

                // user does not have premium, load ads
                mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            } else {
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
                // user has premium, hide ads
                mAdView.setVisibility(View.GONE);
            }
        }
    }

Disclaimer I've never worked with the purchases api before, so this exact code may not work, but the general idea that you're not hiding the ads after checking for the purchase is right.