我对应用内购买的工作原理感到非常困惑。我已经阅读了文档,但可能错过了某些内容,因为我不明白我需要在我的应用程序中实现什么最后一步才能使其工作。应用内购买功能很好,但是如果用户在将来的某个日期卸载我的应用并重新安装它,我的应用程序就不知道如何确定应用内购买是否以前已经完成。以下是我主类中的代码片段,我试图在其中处理所有这些内容:
@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"});
之前的代码在我妹妹的手机上(SGS3
, ICS
) 崩溃了,但在我的手机(GN
, ICS
, 和 JB
)上却没有。我在第一个活动的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