我正在尝试将InAppBilling功能添加到我的Android应用程序中。我只是想知道在进行alfa测试之前,如果我用SharedPreferences添加项目(这是一个int变量)就足够了,并用这种方式用SharedPreferences保存它:
private static final String HINT = "Hint";
private static final String VALUE = "VALUE";
int hints;
private static final String TAG =
"com.example.game";
com.example.game.util.IabHelper mHelper;
static final String ITEM_SKU = "com.lots.hints";
private Button clickButton;
private Button buyButton;
@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 =
"<my base64 code>";
mHelper = new com.example.game.util.IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
com.example.game.util.IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(com.example.game.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.example.game.util.IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new com.example.game.util.IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(com.example.game.util.IabResult result,
com.example.game.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.example.game.util.IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new com.example.game.util.IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(com.example.game.util.IabResult result,
com.example.game.util.Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
mConsumeFinishedListener);
}
}
};
com.example.game.util.IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new com.example.game.util.IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(com.example.game.util.Purchase purchase,
com.example.game.util.IabResult result) {
if (result.isSuccess()) {
hints = hints + 50;
clickButton.setEnabled(true);
} else {
// handle error
}
Toast.makeText(Pontpiac.this, "You have "+hints + " piece of hints!",
Toast.LENGTH_LONG).show();
}
};
@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.example.game.util.Security.generatePublicKey(base64PublicKey);
return com.example.game.util.Security.verify(key, signedData, signature);
}
@Override
protected void onPause() {
SharedPreferences sp = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putInt(VALUE, hints);
et.commit();
super.onPause();
}
@Override
protected void onStop() {
SharedPreferences sp = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putInt(VALUE, hints);
et.commit();
super.onStop();
}
}
我很困惑,也不知道在哪里添加商品。仅在IabHelper.OnConsumeFinishedListener方法中? 如果有人知道,请回复!