重新安装或删除用户首选项后验证应用内购买

时间:2011-08-13 17:04:15

标签: android in-app-billing

我已经从this tutorial.实施了应用内结算。要购买的商品是设置自定义背景的功能。它工作得很好,但是当我卸载并重新安装应用程序(或清除用户首选项)时,我无法确定如何验证某人是否已经购买了应用内商品。

    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()){这样的东西时,如果它不在处理程序中,我会收到一个强制关闭。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果已清除首选项/数据,则需要恢复事务。这将向您发送用户购买的项目的信息,您可以照常处理授权。阅读reference并查看Dungeons示例,了解如何执行此操作。

答案 2 :(得分:0)

你可以简单地使用Fire-base login&带有字段premium_enabled的实时数据库 - 真/假(如果用户使用旧版谷歌ID为Play商店的另一部手机,这将有所帮助)

一步一步

在您的应用中添加Google登录信息。 在fire-base user_id / email_id和premium_enabled字段中输入用户条目并在其中存储数据。 (您可以根据自己的要求添加字段)

如果购买用户而不是进行输入premium_enabled - true&还使用共享首选项进行离线检查