1

我在 App 中实现了 Google。发布应用程序后它工作正常。但我面临一个问题。当用户安装应用程序然后购买项目然后卸载应用程序并重新安装并转到购买的项目然后应用程序再次锁定购买的项目并询问购买请求。OnRestoreTransaction 有什么问题吗?我使用了来自 Google In app 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);
                    }
            }
            }
        });

编辑:

我在应用内 java 文件的 onCrearte 中调用它:首先检查当前用户是否购买了他要求的商品,如果没有,则发起购买请求。在 onPurchsestateChanged 我只检查状态是否已购买。

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


    }
4

1 回答 1

1

为了恢复所有购买的物品,您必须明确调用mBillingService.restoreTransactions(). 然后,您将收到onPurchaseStateChange每个先前购买的物品的回电。

将此代码放入onCreate方法中,它应该可以正常工作:

if (!getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
    mBillingService.restoreTransactions();
}
于 2013-01-09T05:14:32.223 回答