2017-10-18 68 views
-2

我試圖讓例子Play結算應用程序的描述here的onDestroy實現收帳單客戶

在最後一步,他們所描述

清潔所有的資源和註銷觀察者,你只需要調用BillingClient.endConnection。所以定義裏面BillingManager此調用的方法,然後從GamePlayActivity.onDestroy調用它:根據上述資料

我已經做功能BillingManager Java類稱爲destroy這樣。

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

我的全BillingManager類是像下面

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就像下面

public class GamePlayActivity extends FragmentActivity implements BillingProvider { 

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

// I want call method here 
    } 

} 

現在,我想在我的遊戲活動撥打以上功能。我不知道如何調用它。

+1

'新BillingManager()。destroy()方法'將是一個良好的開端的一個實例...很難回答沒有看到其他地區這個課程以及你如何與之交流。請提供一個[mcve] –

+0

@ cricket_007它可以使用鑄造活動也可以做 –

+0

@AmitVaghela我已添加完整代碼請檢查它 – Priya

回答

2

因爲它在文檔中提到的從GamePlayActivity.onDestroy

通話,但你定義了自己的方法。

重寫onDestroy GamePlayActivity的方法,並將mBillingClient.endConnection();放入其中。

@Override 
protected void onDestroy() { 
    mBillingClient.endConnection(); 
} 
+0

你好!我想打我上面寫的自己的方法....我該怎麼辦? – Priya

+0

也可以在onDestroy –

+0

@Priya中調用其他銷燬方法,這真的沒有必要。此外,這是一個方法調用 - 所以只是這樣做 –

0

我假設你的活動已經有BillingManager

public class GamePlayActivity extends FragmentActivity implements BillingProvider { 

    BillingManager bm; // assign this in onCreate 

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

}