未从 MarketBillingService 获取 onServiceDisconnected

发布于 2024-12-18 21:03:05 字数 1303 浏览 0 评论 0原文

当我尝试根据 Google Dungeons 示例实现应用内购买时,似乎出现了资源泄漏。

当 MarketbillingService 在 unbind() 函数中调用 unbindService() 后没有调用 onServiceDisconnected() 时(所有这些都在 BillingService 类中),就会出现问题。

所以我的问题是:如何将 ServiceConnection 发布到 MarketBillingService?

/**
 * This class sends messages to Android Market on behalf of the application by
 * connecting (binding) to the MarketBillingService. The application
 * creates an instance of this class and invokes billing requests through this service.
 *
 * The {@link BillingReceiver} class starts this service to process commands
 * that it receives from Android Market.
 *
 * You should modify and obfuscate this code before using it.
 */
public class BillingService extends Service implements ServiceConnection {

 [...]

/**
 * Unbinds from the MarketBillingService. Call this when the application
 * terminates to avoid leaking a ServiceConnection.
 */
public void unbind() {
    try {
        unbindService(this);
    } catch (IllegalArgumentException e) {
        // This might happen if the service was disconnected
    }
}


/**
 * This is called when we are disconnected from the MarketBillingService.
 */
public void onServiceDisconnected(ComponentName name) {
    Log.w(TAG, "Billing service disconnected");
    mService = null;
}

It seems that I get a resource leak when I try to implement in-app purchase according to the Google Dungeons example.

The problem arises when the MarketbillingService does not call onServiceDisconnected() after a call to unbindService() in the unbind() function (All of which are in the BillingService class).

So my problem is this: How do I release the ServiceConnection to the MarketBillingService?

/**
 * This class sends messages to Android Market on behalf of the application by
 * connecting (binding) to the MarketBillingService. The application
 * creates an instance of this class and invokes billing requests through this service.
 *
 * The {@link BillingReceiver} class starts this service to process commands
 * that it receives from Android Market.
 *
 * You should modify and obfuscate this code before using it.
 */
public class BillingService extends Service implements ServiceConnection {

 [...]

/**
 * Unbinds from the MarketBillingService. Call this when the application
 * terminates to avoid leaking a ServiceConnection.
 */
public void unbind() {
    try {
        unbindService(this);
    } catch (IllegalArgumentException e) {
        // This might happen if the service was disconnected
    }
}


/**
 * This is called when we are disconnected from the MarketBillingService.
 */
public void onServiceDisconnected(ComponentName name) {
    Log.w(TAG, "Billing service disconnected");
    mService = null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文