我在应用程序中实现了Google。它在发布应用程序后运行良好。但我正面临着一个问题。当用户安装应用程序,然后购买项目,然后卸载应用程序,重新安装,并进入购买的项目,然后应用程序锁定购买项目,并要求购买请求。OnRestoreTransaction有什么问题吗?我在应用程序Purchse网站上使用了谷歌的代码。以下是代码:
private class DungeonsPurchaseObserver extends PurchaseObserver {
public DungeonsPurchaseObserver(Handler handler) {
super(in_app.this, handler);
}
@Override
public void onBillingSupported(boolean supported) {
if (Consts.DEBUG) {
Log.i(TAG, "supported: " + supported);
}
if (supported) {
restoreDatabase();
} else {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
if (purchaseState == PurchaseState.PURCHASED ) {
finish();
}
}
@Override
public void onRequestPurchaseResponse(RequestPurchase request,
ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
}
}
@Override
public void onRestoreTransactionsResponse(RestoreTransactions request,
ResponseCode responseCode) {
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.d(TAG, "completed RestoreTransactions request");
}
// Update the shared preferences so that we don't perform
// a RestoreTransactions again.
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(DB_INITIALIZED, true);
edit.commit();
} else {
if (Consts.DEBUG) {
Log.d(TAG, "RestoreTransactions error: " + responseCode);
}
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.in_app);
mHandler = new Handler();
mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
mPurchaseDatabase = new PurchaseDatabase(this);
//setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mDungeonsPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
purchase=(Button) findViewById(R.id.purchase);
cancel=(Button) findViewById(R.id.cancel);
purchase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(!ownedItems.contains("android.test.refunded")){
if (Consts.DEBUG) {
Log.d(TAG, "buying: " + " product" + " Product Name: " + "Product");
}
if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
}
});编辑:
我将其命名为onCrearte of iniate java文件:这首先检查当前用户是否购买了他所要求的商品,如果不是,则拒绝购买请求。在onPurchsestateChanged中,我只检查状态是否已购买。
requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN( super.onCreate( savedInstanceState);setContentView(R.layout.in_app);
mHandler = new Handler();
mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
mPurchaseDatabase = new PurchaseDatabase(this);
//setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mDungeonsPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
System.out.println("for check"+getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false));
if (getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
System.out.println(mBillingService.restoreTransactions());
}
else{
purchase=(Button) findViewById(R.id.purchase);
cancel=(Button) findViewById(R.id.cancel);
purchase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(!ownedItems.contains("android.test.refunded")){
if (Consts.DEBUG) {
Log.d(TAG, "buying: " + " product" + " Product Name: " + "i");
}
if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
startActivity(new Intent(in_app.this, Main.class));
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
}
});
}
}OnPurchaseStateChanged:
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
System.out.println("here for new fun");
if (purchaseState == PurchaseState.PURCHASED ) {
finish();
Intent intent1 = new Intent(context,BodyParts1.class);
context.startActivity(intent1);
}
}发布于 2013-01-09 05:14:32
为了恢复所有购买的物品,您必须显式地调用mBillingService.restoreTransactions()。然后,您将收到每个先前购买的项目的onPurchaseStateChange回调。
将此代码放入onCreate方法中,它应该工作得很好:
if (!getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
mBillingService.restoreTransactions();
}https://stackoverflow.com/questions/14228460
复制相似问题