我应该添加什么来恢复购买的" premium"?我还有一些不应该恢复的元素。我正在使用此https://github.com/googlesamples/android-play-billing/tree/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/util
PurchaseActivity.class
public class PurchaseActivity extends AppCompatActivity {
final String OBJECT_TO_BUY = "this_app_object"; //not restorable
final String PREMIUM = "this_app_premium"; //to restore
public IabHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.purchases);
String base64EncodedPublicKey = "<my_wonderful_base64encodedpublicKey>";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {}
});
}
public void purchasePremium(View v) {
mHelper.launchPurchaseFlow(this, PREMIUM, 10001, mPurchaseFinishedListener, "premium");
}
public void purchaseObject(View v) {
mHelper.launchPurchaseFlow(this, OBJECT_TO_BUY, 10001, mPurchaseFinishedListener, "object");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase) {
if (result.isFailure()) {
return;
} else if (purchase.getSku().equals(PREMIUM)) {
consumeItem();
} else if (purchase.getSku().equals(OBJECT_TO_BUY) {
consumeItem();
}
}
};
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(PREMIUM),
mConsumeFinishedListener);
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
if (purchase.getSku() == PREMIUM) getSharedPreferences("MyApp", Context.MODE_PRIVATE).edit().putBoolean("Premium", true).apply();
if (purchase.getSku() == OBJECT_TO_BUY) {
int objects = getSharedPreferences("MyApp", Context.MODE_PRIVATE).getInt("objects", 0);
getSharedPreferences("MyApp", Context.MODE_PRIVATE).edit().putInt("objects", objects++).apply();}
} else {
// nothink
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
}