在应用程序购买中不保存共享首选项

时间:2013-08-09 12:15:52

标签: java android android-activity in-app-purchase sharedpreferences

我有一个类似于命运之轮的Android应用程序,用户可以选择购买一个耗材,1000美元和两个权利,他们将两个图像解锁为轮式。我正在使用Amazon In-App Purchasing API。用户应该能够购买任意数量的消耗品,但是一旦他们购买了权利,解锁的图像应该是他们看到的唯一图像,并且他们不应再看到锁定的图像。这些应用内购买在我发起这些购买的第一个实例中运行良好。 然而,消耗品领域将仅更新一次,即使我仍然可以完成消费品的购买过程,包含分数或金钱的文本视图在第一次初始购买之后不会更新。此外,车轮返回到锁定的图像,而不是作为解锁的图像保留,尽管当我开始购买这些权利时,我被告知我已经拥有这些物品。因此,我认为这可能与我的SharedPreferences有关。简而言之,我的购买一次更新我的观点,然后再次更新,但后端代码,即我在完成购买时从亚马逊客户端收到的回复是正确的。任何人都可以看到我犯了错误吗?为什么包含得分的textView会在第一次购买时更新,而从那时起再也不会更新?另外如何保存轮子样式的更改,以便在重新打开时他们不再有购买轮子的选项?我有三个课程,并包含以下代码。非常感谢所有和任何帮助。

游戏类

public class Game extends Activity {

private ImageView wheel;
private int rand;
private int[] amounts = {100,650,-1,650,300,-1,800,250,-1,500};
private int score = 0;
private TextView scoreText;
private AnimatorSet set;
protected boolean animationDone = true;
private SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    prefs.registerOnSharedPreferenceChangeListener(prefsChanged);

    wheel = (ImageView) findViewById(R.id.imageView1);
    scoreText = (TextView) findViewById(R.id.score);

    score = prefs.getInt("score", 0);       
    scoreText.setText("$" + String.valueOf(score));
}

private OnSharedPreferenceChangeListener prefsChanged = new OnSharedPreferenceChangeListener() {

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        if(key.equals("money") && prefs.getBoolean(key, false)) {
            score += 1000;
            scoreText.setText("$" + String.valueOf(score));
            prefs.edit().putBoolean("money", false);
        }
    }
};

@Override
protected void onStart() {
    super.onStart();
    InAppObserver obs = new InAppObserver(this);
    PurchasingManager.registerObserver(obs);
}

@Override
protected void onPause() {
    if(this.isFinishing())
    {
        prefs.edit().putInt("score", score).commit();
    }
    super.onPause();
}

@Override
protected void onStop() {
    prefs.edit().putInt("score", score).commit();       
    super.onStop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode != RESULT_CANCELED) {
        String style = data.getStringExtra("wheel");
        if(style.equals("camo"))
            wheel.setImageResource(R.drawable.camowheel);
        if(style.equals("gold"))
            wheel.setImageResource(R.drawable.goldwheel);
        if(style.equals("normal"))
            wheel.setImageResource(R.drawable.wheel);
    }
}

public void spinTheWheel(View v) {
    if(animationDone) {
        wheel.setRotation(0);
        rand = (int) Math.round(2000 + Math.random()*360);
        set = new AnimatorSet();
        set.play(ObjectAnimator.ofFloat(wheel, View.ROTATION, rand));
        set.setDuration(2000);
        set.setInterpolator(new DecelerateInterpolator());
        set.start();
        animationDone = false;
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                calculateResult();
                animationDone = true;
            }
        });
    }       
}

private void calculateResult() {
    int angle = (int) wheel.getRotation();
    angle %= 360;
    angle = (int) Math.floor(angle/36);
    if(amounts[angle] == -1) {
        Intent intent = new Intent(this, GameOver.class);
        intent.putExtra("score", score);
        prefs.edit().putInt("score", 0).commit();
        score = 0;
        startActivity(intent);
    }   
    else {
        score += amounts[angle];
        scoreText.setText("$"+String.valueOf(score));
        prefs.edit().putInt("score", 0).commit();
    }       
}

public void upgradeWheel(View v) {      
    Intent intent = new Intent(getApplicationContext(), ChangeWheel.class);
    startActivityForResult(intent, 1);
}

public void endGame(View v) {
    Intent intent = new Intent(getApplicationContext(), GameOver.class);
    intent.putExtra("score", score);
    prefs.edit().putInt("score", 0).commit();
    score = 0;
    startActivity(intent);
}

public void addMoney(View v) {
    PurchasingManager.initiatePurchaseRequest("money");
}
}

ChangeWheel Class

    public class ChangeWheel extends Activity {

private Button buyCamoButton;
private Button buyGoldButton;
private ImageButton goldButton;
private ImageButton camoButton;
private SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_wheel);

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    prefs.registerOnSharedPreferenceChangeListener(prefsChanged);

    buyCamoButton = (Button) findViewById(R.id.buyCamo);
    buyGoldButton = (Button) findViewById(R.id.buyGold);
    goldButton = (ImageButton) findViewById(R.id.goldButton);
    camoButton = (ImageButton) findViewById(R.id.camoButton);
    goldButton.setEnabled(false);
    camoButton.setEnabled(false);
}

private OnSharedPreferenceChangeListener prefsChanged = new OnSharedPreferenceChangeListener() {

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        if(key.equals("camo") && prefs.getBoolean(key, false)) {
            camoButton.setImageResource(R.drawable.camowheel);
            camoButton.setEnabled(true);
            buyCamoButton.setVisibility(View.INVISIBLE);
        }
        else if(key.equals("gold") && prefs.getBoolean(key, false)) {
            goldButton.setImageResource(R.drawable.goldwheel);
            goldButton.setEnabled(true);
            buyGoldButton.setVisibility(View.INVISIBLE);
        }
    }
};

@Override
protected void onStart() {
    super.onStart();
    InAppObserver obs = new InAppObserver(this);
    PurchasingManager.registerObserver(obs);
}

public void camoClick(View v) {
    Intent intent = new Intent(getApplicationContext(), Game.class);
    intent.putExtra("wheel", "camo");
    setResult(RESULT_OK, intent);
    finish();
}

public void goldClick(View v) {
    Intent intent = new Intent(getApplicationContext(), Game.class);
    intent.putExtra("wheel", "gold");
    setResult(RESULT_OK, intent);
    finish();
}

public void normalClick(View v) {
    Intent intent = new Intent(getApplicationContext(), Game.class);
    intent.putExtra("wheel", "normal");
    setResult(RESULT_OK, intent);
    finish();
}

public void buyCamo(View v) {
    String req = PurchasingManager.initiatePurchaseRequest("camo");
    prefs.edit().putString(req, "camo").commit();
}

public void buyGold(View v) {
    String req = PurchasingManager.initiatePurchaseRequest("gold");
    prefs.edit().putString(req, "gold").commit();
}
}

InAppObserver类

public class InAppObserver extends BasePurchasingObserver {

private SharedPreferences prefs;

public InAppObserver(Activity caller) {
    super(caller);

    prefs = PreferenceManager.getDefaultSharedPreferences(caller.getApplicationContext());
}

@Override
public void onSdkAvailable(boolean isSandboxMode) {
    PurchasingManager.initiatePurchaseUpdatesRequest(Offset.BEGINNING);
}

@Override
public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse res) {
    for(String sku : res.getRevokedSkus()) {
        prefs.edit().putBoolean(sku, false).commit();
    }
    switch (res.getPurchaseUpdatesRequestStatus()) {
    case SUCCESSFUL:
        for(Receipt rec : res.getReceipts()) {
            prefs.edit().putBoolean(rec.getSku(), true).commit();
        }
        break;
    case FAILED:
        // do something
        break;
    }
}

@Override
public void onPurchaseResponse(PurchaseResponse res) {
    switch(res.getPurchaseRequestStatus()) {
    case SUCCESSFUL:
        String sku = res.getReceipt().getSku();
        prefs.edit().putBoolean(sku, true).commit();
        break;
    case ALREADY_ENTITLED:
        String req = res.getRequestId();
        prefs.edit().putBoolean(prefs.getString(req, null), true).commit();
        break;
    case FAILED:
        // do something
        break;
    case INVALID_SKU:
        // do something
        break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

可能是您没有使用相同的编辑器。

preferences.edit().putString(PreferenceKey.DISTANCE, distance);
preferences.edit().commit();
  

正在返回两个不同的SharedPreferences.Editors。因此   价值没有提交。相反,你必须使用:

SharedPreferences.Editor spe = preferences.edit();
spe.putString(PreferenceKey.DISTANCE, distance);
spe.commit();

从... SharedPreferences not working across Activities