Android Google Play 內購代碼集成 implementation 'com.android.billingclient:billing:2.0.3'

1.引用資源

 implementation 'com.android.billingclient:billing:2.0.3'

2.初始化,購買後會回調onPurchasesUpdated 方法 並調用消耗 方法

    //init
    private BillingClient billingClient;
    private SkuDetails mSkuDetails;
    private void initGoogleBillingManager(){

        billingClient = BillingClient.newBuilder(MenuActivity.this).enablePendingPurchases().setListener(new PurchasesUpdatedListener() {
            @Override
            public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
                LogTools.i(TAG,"onPurchasesUpdated=getResponseCode=="+billingResult.getResponseCode());
                if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK) {

                    //消耗品 開始消耗
                    ConsumeParams.Builder consumeParams = ConsumeParams.newBuilder();
                    consumeParams.setPurchaseToken(purchases.get(0).getPurchaseToken());
                    consumeParams.setDeveloperPayload(purchases.get(0).getDeveloperPayload());
                    billingClient.consumeAsync(consumeParams.build(), new ConsumeResponseListener() {
                        @Override
                        public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                            LogTools.i(TAG,"onConsumeResponse=getResponseCode=="+billingResult.getResponseCode());

                            // Process the result.
                            if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                                LogTools.i(TAG,"onConsumeResponse=BillingClient.BillingResponseCode.OK==");

                            }else{
                                LogTools.i(TAG,"onConsumeResponse=getDebugMessage=="+billingResult.getDebugMessage());
                            }
                        }
                    });

                    //handle

                }else{
                    LogTools.i(TAG,"onPurchasesUpdated=getDebugMessage=="+billingResult.getDebugMessage());
                }
            }

        }).build();


        //connect google service
        startConnectionBillingService();

    }

3.連接服務 並 查詢商品id

//connect
    private void startConnectionBillingService(){
        if(billingClient!=null) {
            billingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(BillingResult billingResult) {
                    LogTools.i(TAG,"onBillingSetupFinished=getResponseCode=="+billingResult.getResponseCode());
                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                        // The BillingClient is ready. You can query purchases here.
                         
                        //根據商品id 查詢
                        List<String> skuList = new ArrayList<> ();
                        skuList.add("remove_ads");
                        SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                        params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);

                        billingClient.querySkuDetailsAsync(params.build(),
                                new SkuDetailsResponseListener() {
                                    @Override
                                    public void onSkuDetailsResponse(BillingResult billingResult,List<SkuDetails> skuDetailsList) {
                                        LogTools.i(TAG,"querySkuDetailsAsync=getResponseCode=="+billingResult.getResponseCode()+",skuDetailsList.size="+skuDetailsList.size());
                                        // Process the result.
                                        if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                                            mSkuDetails=skuDetailsList.get(0);
                                            LogTools.i(TAG,"querySkuDetailsAsync=mSkuDetails.toString=="+mSkuDetails.toString());

                                        }else{
                                            LogTools.i(TAG,"querySkuDetailsAsync=getDebugMessage=="+billingResult.getDebugMessage());
                                        }
                                    }
                                });
                    }else{

                        LogTools.i(TAG,"onBillingSetupFinished=getDebugMessage=="+billingResult.getDebugMessage());
                    }
                }

                @Override
                public void onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    LogTools.i(TAG,"onBillingServiceDisconnected=");

                }
            });
        }
    }

4.購買

    //buy
    public void googlePay(Activity activity,SkuDetails skuDetails) {
        if(billingClient!=null){

            BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                    .setSkuDetails(skuDetails)
                    .build();
            billingClient.launchBillingFlow(activity,flowParams);

        }
    }

5.在混淆文件添加混淆配置

-keep class com.android.vending.billing.**

 

6.添加測試賬號

  

7.發佈這個測試版本,顯示發佈成功後(一般兩個小時) 加入測試網址 

官方開發文檔:https://developer.android.com/google/play/billing/billing_library_overview 

最後附上本人開發的一些,小遊戲:https://play.google.com/store/apps/details?id=com.nooogfaaar.flowercolor

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章