1

我正在尝试按照本教程http://blog.blundell-apps.com/simple-inapp-billing-payment/了解应用内购买

到目前为止,这是我的代码:

public class main extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("BillingService", "Starting");
        setContentView(R.layout.main);

        startService(new Intent(this, BillingService.class));
        BillingHelper.setCompletedHandler(mTransactionHandler);

    }

    ////////////////////////////////

    public Handler mTransactionHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {

                Log.e("IN APP", "Transaction complete");
                Log.e("IN APP", "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
                Log.e("IN APP", "Item purchased is: "+BillingHelper.latestPurchase.productId);

                if(BillingHelper.latestPurchase.isPurchased())
                {
                    showItem();
                }

            };

    };

    ////////////////////////////////

    public void BuyButtonClick(View v) {

        if(BillingHelper.isBillingSupported()){
            Log.e("IN APP","Trying to buy...");
            BillingHelper.requestPurchase(this, "android.test.purchased"); 
        } else {
            Log.e("IN APP","Can't purchase on this device");
        }

    }

    ////////////////////////////////////////////////

    private void showItem() {

        TextView tv1 = (TextView)findViewById(R.id.tv1);
        tv1.setText("PAID!");

    }

    ////////////////////////////////////////////////

    @Override
    protected void onDestroy() {
        BillingHelper.stopService();
        super.onDestroy();
    }

    ////////////////////////////////

}

一切似乎都很好,但我还想要一些方法来检查应用程序启动时是否购买了该项目。我认为它可能会使用BillingHelper.verifyPurchase(signedData, signature),但是我应该在那里输入什么数据和签名?或者也许还有其他方法?

谢谢!

4

1 回答 1

0

您可以使用restoreTransactions检查应用程序何时启动。如果您使用了托管产品或订阅,那么只有您才能获得用户的所有详细信息。

对于非托管产品,谷歌没有维护任何细节。

所以在你的主要活动中调用它

mBillingService = new BillingService();
mBillingService.setContext(this);
mBillingService.restoreTransactions();

一旦你在 ResponseHandler 类中调用它,就会有一种方法purchaseResponse

purchaseResponse(final Context context,
        final PurchaseState purchaseState, final String productId,
        final String orderId, final long purchaseTime,
        final String developerPayload, final String purchaseToken) {
 }

这将返回所有详细信息。

您可以在之后检查 purchaseState

           if (purchaseState == PurchaseState.PURCHASED) {

            } else if (purchaseState == PurchaseState.REFUNDED) {

            } else if (purchaseState == PurchaseState.CANCELED) {

            }
于 2012-12-10T10:51:16.047 回答