0

我已经从本教程中实现了应用内计费。要购买的项目是设置自定义背景的能力。效果很好,但是当我卸载并重新安装应用程序(或清除用户首选项)时,我无法弄清楚如何验证是否有人已经购买了应用程序内商品。

    public void buySelected() {

    if (backgroundColorsPurchased == true) {
        this.colorChangeDialog(); //if user has already purchased, just call the dialog instead of re-buying.
        //if the person has cleared their prefs, they'll have to be online to re-verify that they did indeed buy the item.

    }else{
        if(BillingHelper.isBillingSupported()){
            BillingHelper.requestPurchase(mContext, "background.colors");
            BillingHelper.setCompletedHandler(mTransactionHandler);

        } else {
            Log.i(TAG,"Can't purchase on this device");

        }
    }
}

然后我有处理程序:

    public Handler mTransactionHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        Log.i(TAG, "Transaction complete");
        Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
        Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

        if(BillingHelper.latestPurchase.isPurchased()){
        //this is where we show the stuff that the person purchased. In this case, the dialog to change the background colour.
            backgroundColorsPurchased = true; //just setting this to true so that the next time somebody clicks the donate button it'll just open the dialog.

            //call the change background dialog
            colorChangeDialog();

        }else{
            //fail
            Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();

        }
    }

 };

我如何能够验证该物品之前是否已购买?市场只是不断弹出一个对话窗口,上面写着“您已经购买了该商品,或者该购买仍在等待中”。当我尝试使用类似的东西时,if(BillingHelper.latestPurchase.isPurchased()){如果它不在处理程序中,我会强制关闭。

4

3 回答 3

0

您需要调用 restoreTransactions 查看 Android 为 In App Billing 提供的默认示例以获取更多参考。

于 2011-08-13T17:08:59.743 回答
0

如果首选项/数据已被清除,您需要恢复交易。这将向您发送用户购买的物品的信息,您可以照常处理授权。阅读参考资料并查看 Dungeons 示例以了解如何执行此操作。

于 2011-08-13T17:10:08.357 回答
0

您可以简单地使用基于 Fire-base 登录和实时数据库的字段 premium_enabled - true/false(如果用户使用旧 google id 使用 Play-store 的另一部手机,这将有所帮助)

循序渐进

在您的应用程序中添加谷歌登录。在 fire-base user_id/email_id 和 premium_enabled 字段中输入用户并将数据存储在其中。(您可以根据需要添加字段)

如果用户购买而不是输入 premium_enabled - true 并且还使用共享偏好进行离线检查

于 2018-02-12T11:36:00.177 回答