Android - 如何验证应用内购买?

时间:2012-11-23 20:23:00

标签: android

我正在尝试按照本教程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),但我应该放入哪些数据和信号?或者可能还有其他一些方法吗?

谢谢!

1 个答案:

答案 0 :(得分: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) {

            }