Android 应用内计费,如何处理应用重新安装

标签 android android-billing

我对应用内结算的运作方式感到很困惑。我已经阅读了文档,我一定错过了一些东西,因为我不明白我需要在我的应用程序中实现以使其工作的最后一步。应用内计费效果很好,但是,如果用户卸载我的应用并在以后再次安装,我的应用不知道如何确定之前是否进行过应用内购买。这是我的主要类(class)的一个 fragment ,我试图在其中处理所有这些:

@Override
public void onCreate(Bundle savedInstanceState)
{
    mContext = this;
    startService(new Intent(mContext, BillingService.class));
    BillingHelper.setCompletedHandler(mTransactionHandler);
}

我正在使用地牢示例项目中的示例类。我不明白的是下面的代码在购买时是如何工作的,但是重新运行它并不能检查是否已经购买了某些东西。我已经在这部分停留了大约一个月,对此我感到非常沮丧。

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())
        {
            new Message("Thank you!", getApplicationContext());
            PAY_VERSION = true;
            SharedPreferences purchasePreferences = getSharedPreferences(PURCHASE_PREFERENCES, Activity.MODE_PRIVATE);
            Editor purchaseEditor = purchasePreferences.edit();
            purchaseEditor.putBoolean("purchased", PAY_VERSION);
            purchaseEditor.commit();
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    };

我需要的是某种方式来查询服务器以查看该商品是否已被购买。我知道某处有一个 PURCHASE_STATE_CHANGED 东西,但是我不知道当它确定状态已更改时如何做任何事情或如何启动它进行检查。我迷路了,我所需要的只是朝着正确的方向大力插入,因为到目前为止我完全迷路了。

编辑:

我还听说您需要解析 JSON,但我什至不知道如何开始这样做。

编辑 2:

我应该调用这些东西来检查吗?

BillingHelper.restoreTransactionInformation(BillingSecurity.generateNonce());
BillingHelper.getPurchaseInformation(new String[] {"myItem"});

该代码之前曾在我姐妹的手机(SGS3ICS)上崩溃,但在我的(GNICSJB 工作)。我在第一个 Activity 的 onCreate() 中调用它。不太确定调用 getPurchaseInformation(...) 后如何处理。它没有返回值,所以我不确定我是否可以解析 JSON 或我应该做的任何事情......

另外,这两行给了我这个:

08-27 11:54:04.271: E/BillingService(17702): BillingHelper not fully instantiated
08-27 11:54:04.271: E/BillingService(17702): BillingHelper not fully instantiated

最佳答案

JSON

包含订阅购买 token 的 JSON 订单对象示例如下所示。

{ "nonce" : 1836535032137741465,

  "orders" :
    [{ "notificationId" : "android.test.purchased",
       "orderId" : "transactionId.android.test.purchased",
       "packageName" : "com.example.dungeons",
       "productId" : "android.test.purchased",
       "developerPayload" : "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
       "purchaseTime" : 1290114783411,
       "purchaseState" : 0,
       "purchaseToken" : "rojeslcdyyiapnqcynkjyyjh" }]
}

如何在 Java 中解析 JSON .

Android supports JSON

Android 结算

如果您有远程服务器,我们建议您将购买信息存储在您的服务器上,而不是存储在设备的本地数据库中。 Look here .

了解 Managed per user account, Unmanaged, or Subscription

我使用这样的计费方式:

  1. 在onCreate中

     BillingHelper.setCompletedHandler(handlerTransaction);
    

    您的代码中有处理程序。

  2. onClick 中下一步

     BillingHelper.requestPurchase(this, currentMarketProduct.getMarketId());
    

关于Android 应用内计费,如何处理应用重新安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12146508/

相关文章:

android - 如何设置软键盘到EditText的距离?

android - 日期验证不起作用

in-app-purchase - 是否有必要从 "Google Play Billing with AIDL"迁移到 "Google Play Billing Library"?

android - 发送短信时查看确认充值账单对话框

Android 应用内结算 "Un-purchase"用于测试?

java - SimpleCursorAdapter 无法在 ListActivity 中工作,日志中没有错误

android - 在 Android 上从 Webview 创建 PDF

android - 查询购买商品时获取订单ID

android - 从 firebase 数据库检索数据到 textview 时如何创建换行符?

Android 计费 v3 - BillingClientImpl.java 中的 NullPointerException :668