问题
I'm writing an application for android and I have difficulties with saving int variable with SharedPreferences when using In-App billing. I'd like to add 50 points to my int variable and then save it with SharedPreferences. When the purchase is finished the user got nothing, but it said Payment Succesfull.How should I add and save the 50 points?
Here is my code:
private static final String HINT = "Hint";
private static final String VALUE = "VALUE";
int hints;
private static final String TAG =
"com.game.example";
com.game.example.util.IabHelper mHelper;
static final String ITEM_SKU = "com.fifty.points";
private Button clickButton;
private Button buyButton;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pontpiac);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_pontpiac);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.hide();
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
hints = sphint.getInt(VALUE, 0);
buyButton = (Button)findViewById(R.id.buyButton);
clickButton = (Button)findViewById(R.id.clickButton);
clickButton.setEnabled(false);
String base64EncodedPublicKey =
"Base64 code;
mHelper = new com.game.example.util.IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
com.game.example.util.IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(com.game.example.util.IabResult result) {
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " +
result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
}
public void buttonClicked (View view)
{
clickButton.setEnabled(false);
buyButton.setEnabled(true);
}
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "mypurchasetoken");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
com.game.example.util.IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new com.game.example.util.IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(hu.szada.kepkirako.util.IabResult result,
com.game.example.util.Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
buyButton.setEnabled(false);
}
}
};
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
com.game.example.util.IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new com.game.example.util.IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(com.game.example.util.IabResult result,
com.game.example.util.Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
mConsumeFinishedListener);
}
}
};
@Override
protected void onResume() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
hints = sphint.getInt(VALUE, 0);
super.onResume();
}
com.game.example.util.IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new com.game.example.util.IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(hu.szada.kepkirako.util.Purchase purchase,
com.game.example.util.IabResult result) {
if (result.isSuccess()) {
hints = hints + 50;
clickButton.setEnabled(true);
} else {
// handle error
}
}
};
@Override
protected void onPause() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
Toast.makeText(Pontpiac.this, "" + hints,
Toast.LENGTH_LONG).show();
super.onPause();
}
@Override
protected void onStop() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
public static boolean verifyPurchase(String base64PublicKey,
String signedData, String signature) {
if (TextUtils.isEmpty(signedData) ||
TextUtils.isEmpty(base64PublicKey) ||
TextUtils.isEmpty(signature)) {
Log.e(TAG, "Purchase verification failed: missing data.");
if (BuildConfig.DEBUG) {
return true;
}
return false;
}
PublicKey key = com.game.example.util.Security.generatePublicKey(base64PublicKey);
return hu.szada.kepkirako.util.Security.verify(key, signedData, signature);
}}
回答1:
TinyDB -- Android-Shared-Preferences-Turbo
This class simplifies calls to SharedPreferences in a line of code. It can also do more like: saving a list of strings, integers and saving images. All in 1 line of code!
Example usage:
TinyDB tinydb = new TinyDB(context);
tinydb.putInt("clickCount", 2);
tinydb.putFloat("xPoint", 3.6f);
tinydb.putLong("userCount", 39832L);
tinydb.putString("userName", "john");
tinydb.putBoolean("isUserMale", true);
tinydb.putList("MyUsers", mUsersArray);
tinydb.putImagePNG("DropBox/WorkImages", "MeAtlunch.png", lunchBitmap);
//These plus the corresponding get methods are all included
This is just an example of how easy it is to use. There are many more usefull methods included in the class. Enjoy :)
来源:https://stackoverflow.com/questions/32477326/in-app-billing-save-the-item-with-sharedpreferences