Reputation: 107
I am building a game application in which the user have to buy the application in order to unlock more levels. I have implemented the InApp purchase code. But , after testing it for the first time , i can't rerun the test since the item is already bought. I don't want to make my purchase a consumable since it's a one time purchase. How can I check if the user already bought the item ? and how can i test the Buy option multiple times ?
Thanks,
Upvotes: 0
Views: 117
Reputation: 3503
Just an idea, not sure if its good practice though, you could use a Boolean and store it in SharedPreferences. So whenever the in App purchase is triggered you set your Boolean to false and store it in your SharedPreferences, next time the app is opened check if your Boolean is true, if yes enable the purchase :
private SharedPreferences preferences;
private Boolean purchased;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
purchased = preferences.getBoolean("PURCHASED", false);
// will be set to false in case it does not exist yet
if(!purchased)
enableInAppPurchase();
}
When the inAppPurchase method is called store your Boolean:
purchased = true;
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("PURCHASED", purchased);
editor.apply();
This should work, but Im sure theres a better solution.
EDIT : Found a similar question/answer that might help you : How to save the state of inApp purchase
Upvotes: 1