首页 > android > android应用内购买API 3

android应用内购买API 3 (android in-app purchases API 3)

问题

我正在试图弄清楚如何在我的应用程序中实现包含十几项活动的应用程序内购买。主要问题是放置IABHelper m_helper;对象的位置?在单活动应用程序中的一切都很简单(例如示例“android-sdk \ extras \ google \ play_billing \ samples \ TrivialDrive \ src \ com \ example \ android \ trivialdrivesample”)。

但我需要从几个内部与计费系统进行交互ActivitiesIABHelper m_helper;在每个中定义一个是否很酷?我怀疑没有。如果我仅在我的根活动中定义它,我如何从次要活动中访问它?而且,看看这样的场景:

  1. 启动应用。
  2. 根活动启动和计费系统(IABHelper m_helper)设置。
  3. 转到辅助活动。现在我可以访问(以某种方式)root活动,m_helper因为root活动尚未被销毁。
  4. 按主页按钮
  5. 推出一百万款酷炫Android应用。
  6. 返回我的应用程序。次要活动醒来,但Root在第5步被Android杀死。我失去了访问权限m_helper

我错过了一些Android概念?

解决方法

IABHelper提供连接Activity到账单的便捷方法Service。它不是全局连接,因此您当然可以为每个Activity需要读/写帐单信息的人创建一个连接。

但是,请确保您只m_helper.setup(this)为每个活动呼叫一次(通常在该活动onCreateActivity)。同样,m_helper.dispose()每次只调用一次Activity(通常在onDestroy方法中)。

问题

I'm trying to figure out how to implement in-app purchasing in my app that comprises a dozen of activities. The main question is where to put IABHelper m_helper; object? Everything is simple in case on single-activity app (like the sample "android-sdk\extras\google\play_billing\samples\TrivialDrive\src\com\example\android\trivialdrivesample").

But I need to interact with billing system from within several Activities. Is it cool to define a IABHelper m_helper; in each of them? I suspect no. If I define it only in my root activity, how do I access it from secondary activities? Moreover, look at a scenario like this:

  1. Start app.
  2. Root activity launches and billing system (IABHelper m_helper) setups.
  3. Go to secondary activity. Now I can access (somehow) root activity's m_helper because root activity is not destroyed yet.
  4. Press home button
  5. Launch a million of cool Android apps.
  6. Return back to my app. Secondary activity wakes up, but Root is killed by Android on step 5. I lost access to m_helper.

Am missing some Android concepts?

解决方法

IABHelper provides convenience methods for connecting an Activity to the billing Service. It is not a global connection, so you can certainly create one for each Activity that needs to read/write billing information.

However, make sure you only call m_helper.setup(this) once for each Activity (generally in the onCreate for that Activity). Similarly, only call m_helper.dispose() once per Activity (usually in the onDestroy method).

相似信息