2017-10-18 70 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(); 
    } 

}