我正在为Android编写应用程序,在使用应用内结算时,我在使用SharedPreferences保存int变量时遇到了困难。我想在我的int变量中添加50个点,然后使用SharedPreferences保存它。当购买完成后,用户什么都没有,但它说付款成功。我应该如何添加并保存50分?
这是我的代码:
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);
}}
答案 0 :(得分:1)
TinyDB -- Android-Shared-Preferences-Turbo
此类简化了对一行代码中对SharedPreferences的调用。它还可以更像:保存字符串,整数和保存图像的列表。全部在一行代码中!
使用示例:
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 :)