【问题标题】:In-app billing save the item with SharedPreferences应用内结算使用 SharedPreferences 保存项目
【发布时间】:2015-09-09 10:40:01
【问题描述】:

我正在为 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);
}}

【问题讨论】:

  • 你有什么错误吗?
  • 我没明白你的问题。

标签: android in-app-billing


【解决方案1】:

TinyDB -- Android-Shared-Preferences-Turbo

这个类在一行代码中简化了对 SharedPreferences 的调用。它还可以做更多类似的事情:保存字符串列表、整数和保存图像。全部在 1 行代码中!

示例用法:

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 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2018-08-30
    • 2023-03-21
    • 2014-12-25
    • 1970-01-01
    相关资源
    最近更新 更多