首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关闭计费客户端的onDestroy实现

关闭计费客户端的onDestroy实现
EN

Stack Overflow用户
提问于 2017-10-18 05:42:28
回答 2查看 2.5K关注 0票数 1

我正在尝试制作描述这里的播放计费应用程序的例子。

在最后一步,他们描述了

要清理所有资源并注销观察者,只需调用BillingClient.endConnection即可。因此,在BillingManager中使用此调用定义一个方法,然后从GamePlayActivity.onDestroy调用它:

根据上面的信息,我在destroy java类中这样做了BillingManager函数。

代码语言:javascript
运行
复制
public void destroy() {
        mBillingClient.endConnection();
    }

我的完整BillingManager类如下所示

代码语言:javascript
运行
复制
public class BillingManager implements PurchasesUpdatedListener {
    private final BillingClient mBillingClient;
    private final Activity mActivity;
    private static final String TAG = "BillingManager";

    public BillingManager(Activity  activity) {

        mActivity = activity;
        mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
                if (billingResponse == BillingClient.BillingResponse.OK) {
                    Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
                } else {
                    Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
                }
            }
            @Override
            public void onBillingServiceDisconnected() {
                Log.w(TAG, "onBillingServiceDisconnected()");
            }
        });
    }

    public void startPurchaseFlow(final String skuId, final String billingType) {
        // Specify a runnable to start when connection to Billing client is established
        Runnable executeOnConnectedService = new Runnable() {
            @Override
            public void run() {
                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                        .setType(billingType)
                        .setSku(skuId)
                        .build();
                mBillingClient.launchBillingFlow(mActivity, billingFlowParams);
            }
        };

        // If Billing client was disconnected, we retry 1 time
        // and if success, execute the query
        startServiceConnectionIfNeeded(executeOnConnectedService);
    }

    @Override
    public void onPurchasesUpdated(@BillingClient.BillingResponse int responseCode,
                                   List<Purchase> purchases) {
        Log.d(TAG, "onPurchasesUpdated() response: " + responseCode);
    }

    private static final HashMap<String, List<String>> SKUS;
    static
    {
        SKUS = new HashMap<>();
        SKUS.put(BillingClient.SkuType.INAPP, Arrays.asList("gas", "premium"));
        SKUS.put(BillingClient.SkuType.SUBS, Arrays.asList("gold_monthly", "gold_yearly"));
    }

    public List<String> getSkus(@BillingClient.SkuType String type) {
        return SKUS.get(type);
    }

    public void querySkuDetailsAsync(@BillingClient.SkuType final String itemType,
                                     final List<String> skuList, final SkuDetailsResponseListener listener) {
        // Specify a runnable to start when connection to Billing client is established
        Runnable executeOnConnectedService = new Runnable() {
            @Override
            public void run() {
                SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
                        .setSkusList(skuList).setType(itemType).build();
                mBillingClient.querySkuDetailsAsync(skuDetailsParams,
                        new SkuDetailsResponseListener() {
                            @Override
                            public void onSkuDetailsResponse(int responseCode,
                                                             List<SkuDetails> skuDetailsList) {
                                listener.onSkuDetailsResponse(responseCode, skuDetailsList);
                            }
                        });
            }
        };

        // If Billing client was disconnected, we retry 1 time
        // and if success, execute the query
        startServiceConnectionIfNeeded(executeOnConnectedService);
    }

    private void startServiceConnectionIfNeeded(final Runnable executeOnSuccess) {
        if (mBillingClient.isReady()) {
            if (executeOnSuccess != null) {
                executeOnSuccess.run();
            }
        } else {
            mBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
                    if (billingResponse == BillingClient.BillingResponse.OK) {
                        Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
                        if (executeOnSuccess != null) {
                            executeOnSuccess.run();
                        }
                    } else {
                        Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
                    }
                }
                @Override
                public void onBillingServiceDisconnected() {
                    Log.w(TAG, "onBillingServiceDisconnected()");
                }
            });
        }
    }

    public void destroy() {
        mBillingClient.endConnection();
    }
}

我的GamePlayActivity如下所示

代码语言:javascript
运行
复制
public class GamePlayActivity extends FragmentActivity implements BillingProvider {

@Override
    protected void onDestroy() {
        super.onDestroy();

// I want call method here
    }

}

现在我想在我的游戏活动中调用上面的功能。我不知道怎么称呼它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-18 05:47:32

正如它在文件中提到的

从GamePlayActivity.onDestroy打电话来

但你定义了你自己的方法。

重写GamePlayActivity的GamePlayActivity方法并将mBillingClient.endConnection();放入其中。

代码语言:javascript
运行
复制
@Override
protected void onDestroy() {
    mBillingClient.endConnection();
}
票数 2
EN

Stack Overflow用户

发布于 2017-10-18 06:06:22

我假设您的活动已经有了一个BillingManager实例

代码语言:javascript
运行
复制
public class GamePlayActivity extends FragmentActivity implements BillingProvider {

    BillingManager bm;  // assign this in onCreate 

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bm.destroy();
   }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46803491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档