本文介绍了应用内结算保存共享preferences项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的Andr​​oid应用程序,我使用时,应用内计费与使用共享preferences节省int变量的困难。我想50点添加到我的int变量,然后将其保存与共享preferences。当购买完成后,用户什么也没得到,但它表示,支付Succesfull.How我应该添加和保存了50分?

下面是我的code:

 私有静态最后弦乐HINT =提示;
私有静态最后的字符串值=VALUE;
诠释提示;私有静态最后的字符串标记=
        com.game.example;
com.game.example.util.IabHelper mHelper;
静态最后弦乐ITEM_SKU =com.fifty.points;私人按钮clickButton;
私人按钮buyButton;
按钮BTN;
@覆盖
保护无效的onCreate(捆绑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动作条= getSupportActionBar();
    actionbar.hide();
    。共享preferences sphint = getApplicationContext()getShared preferences(HINT,MODE_PRIVATE);
    提示= sphint.getInt(VALUE,0);    buyButton =(按钮)findViewById(R.id.buyButton);
   clickButton =(按钮)findViewById(R.id.clickButton);
    clickButton.setEnabled(假);    字符串base64En codedPublicKey =
            的Base64 code;    mHelper =新com.game.example.util.IabHelper(这一点,base64En codedPublicKey);    mHelper.startSetup(新
                               com.game.example.util.IabHelper.OnIabSetupFinishedListener(){
                                   公共无效onIabSetupFinished(com.game.example.util.IabResult结果){
                                       如果(!result.isSuccess()){
                                           Log.d(TAG,应用内结算设置失败:+
                                                   结果);
                                       }其他{
                                           Log.d(TAG,应用内结算设置OK);
                                       }
                                   }
                               });
}
公共无效buttonClicked(查看视图)
{
    clickButton.setEnabled(假);
    buyButton.setEnabled(真);
}公共无效buyClick(查看视图){
    mHelper.launchPurchaseFlow(这一点,ITEM_SKU,10001,
            mPurchaseFinishedListenermypurchasetoken);
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,
                                意图数据)
{
    如果(!mHelper.handleActivityResult(要求code,
            结果code,数据)){
        super.onActivityResult(要求code,结果code,数据);
    }
}
com.game.example.util.IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        =新com.game.example.util.IabHelper.OnIabPurchaseFinishedListener(){
    公共无效onIabPurchaseFinished(hu.szada.kepkirako.util.IabResult结果,
                                      com.game.example.util.Purchase购买)
    {
        如果(result.isFailure()){
            //处理错误
            返回;
        }
        否则如果(purchase.getSku()。等于(ITEM_SKU)){
            consumeItem();
            buyButton.setEnabled(假);
        }    }
};公共无效consumeItem(){
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}com.game.example.util.IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
        =新com.game.example.util.IabHelper.QueryInventoryFinishedListener(){
    公共无效onQueryInventoryFinished(com.game.example.util.IabResult结果,
                                         com.game.example.util.Inventory库存){        如果(result.isFailure()){
            //处理失败
        }其他{
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU)
                    mConsumeFinishedListener);
        }
    }
};@覆盖
保护无效onResume(){
    。共享preferences sphint = getApplicationContext()getShared preferences(HINT,MODE_PRIVATE);
    提示= sphint.getInt(VALUE,0);    super.onResume();
} com.game.example.util.IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        新com.game.example.util.IabHelper.OnConsumeFinishedListener(){
            公共无效onConsumeFinished(hu.szada.kepkirako.util.Purchase购买,
                                          com.game.example.util.IabResult结果){                如果(result.isSuccess()){                    提示=提示+ 50;                    clickButton.setEnabled(真);
                }其他{
                    //处理错误
                }
            }
        };@覆盖
保护无效的onPause(){    。共享preferences sphint = getApplicationContext()getShared preferences(HINT,MODE_PRIVATE);
    共享preferences.Editor ET2 = sphint.edit();
    et2.putInt(VALUE,提示);
    et2.commit();    Toast.makeText(Pontpiac.this,+提示,
            Toast.LENGTH_LONG).show();    super.onPause();
}@覆盖
保护无效的onStop(){    。共享preferences sphint = getApplicationContext()getShared preferences(HINT,MODE_PRIVATE);
    共享preferences.Editor ET2 = sphint.edit();
    et2.putInt(VALUE,提示);
    et2.commit();    super.onStop();
}@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    如果(mHelper!= NULL)mHelper.dispose();
    mHelper = NULL;
}公共静态布尔verifyPurchase(字符串base64PublicKey,
                                     字符串的signedData,特征码){
    如果(TextUtils.isEmpty(签名数据)||
            TextUtils.isEmpty(base64PublicKey)||
            TextUtils.isEmpty(签字)){
        Log.e(TAG,购买验证失败:丢失的数据。);
        如果(BuildConfig.DEBUG){
            返回true;
        }
        返回false;
    }    公钥密钥= com.game.example.util.Security.generatePublicKey(base64PublicKey);
    返回hu.szada.kepkirako.util.Security.verify(钥匙,签名数据,签字);
}}


解决方案

本类简化了在线路code的调用共享preferences。它也可以做更多的,如​​:保存字符串,整数和保存图像的列表。在所有1号线code的!

实例:

  TinyDB tinydb =新TinyDB(背景);tinydb.putInt(的clickCount,2);
tinydb.putFloat(XPOINT,3.6f);
tinydb.putLong(USERCOUNT位,39832L);tinydb.putString(username的,约翰);
tinydb.putBoolean(isUserMale,真正的);tinydb.putList(MyUsers,mUsersArray);
tinydb.putImagePNG(DropBox的/ WorkImages,MeAtlunch.png,lunchBitmap);//这些再加上相应的get方法都包括在内
这只是它是多么容易用一个例子。有包含在类更多的有用的方法。享受:)

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);
}}
解决方案

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

这篇关于应用内结算保存共享preferences项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:14