-1

How to save button state of in-app billing?

i.e, For example, if a person buys a product using Google in-app billing, The next time he opens the application the button must be in unlocked state. I followed few tutorials and am successful in adding button and making purchase but the button remains active only for one time i.e, when user leaves the app he/she must purchase once again which isn't proper in-app billing.

user
  • 291
  • 1
  • 3
  • 16

2 Answers2

1

Just save the buying state in SharedPreferences when it was successful.

private Boolean isUpgrade(Context context) 
{
    SharedPreferences prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);        
    return prefs.getBoolean("upgrade", false);
}

private void setUpgrade(Context context, Boolean value) 
{
    SharedPreferences prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    Editor edit = prefs.edit();
    edit.putBoolean("upgrade", value);        
    edit.commit();
}
goodm
  • 7,275
  • 6
  • 31
  • 55
0

Use SharedPreferences.

First, call the following method to obtain shared preferences in your application:

SharedPreferences prefs = this.getSharedPreferences("com.your.app", Context.MODE_PRIVATE);

To write/update a preference value:

prefs.edit().putBoolean("buyState", true).commit();

To read a preference value:

prefs.getBoolean("buyState", false);

Note that false is a default value to be returned if there is no value stored in preferences for buyState.

ashatte
  • 5,442
  • 8
  • 39
  • 50