0

我有一个类似于命运之轮的 android 应用程序,用户可以选择购买一个消耗品、1000 美元和两个权利,他们可以将两个图像解锁为轮子样式。我正在使用亚马逊应用内购买 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 类

    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;
    }
}
}
4

1 回答 1

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 不能跨活动工作

于 2014-01-25T19:33:31.867 回答