如何实现应用内支付的 SkuDetailsResponseListener?

How to implement SkuDetailsResponseListener for in-app billing?

我想在我的 Android 应用中包含应用内结算。 在阅读文档时,它说:

"specify a listener which implements the SkuDetailsResponseListener interface. You can then override onSkuDetailsResponse() which notifies the listener when the query finishes"

public class MainActivity extends AppCompatActivity implements SkuDetailsResponseListener {

给出:

@Override
    public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {

    }

但是,这是一个问题,因为它不包括文档中指定的侦听器,即。

billingClient.querySkuDetailsAsync(params.build(),
    new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(BillingResult billingResult,
                List<SkuDetails> skuDetailsList) {
            // Process the result.
        }
    });

当包含 BillingClient 行和侦听器时,显然存在解析符号错误,因为它在任何方法之外,那么您如何解决这个问题?

另外,你会把这个块放在 onCreate() 中吗?

List<String> skuList = new ArrayList<> ();
skuList.add("coins5");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);

欢迎使用 Whosebug!

两种解决方案:

  • 使用文档中指定的侦听器,不要在 class 声明中实现它。

  • 或者保留工具,而不是 billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() { ....

只需使用:billingClient.querySkuDetailsAsync(params.build(), this);

如 Billing API 文档中所述,您还需要实施 BillingClientStateListener 和 PurchaseUpdateListener。 https://developer.android.com/google/play/billing/integrate 以下代码是 Billing API 3.0 和 Android Version Targeted SDK 28 中的一个工作示例。该代码是在 Singleton class 中定义的,因此它可以在具有一个实例的多个活动中使用BillingClient.

     public class ApplicationBillingClient
       {
           static ApplicationBillingClient applicationBillingClient= null;
           private static BillingClient billingClient;

          private ApplicationBillingClient() {}

          private static boolean isInitialized()
     {
    return applicationBillingClient != null && billingClient != null;
}

private static void initialize(Context applicationContext)
{
    try
    {
        if(applicationContext != null)
        {
            applicationBillingClient = new ApplicationBillingClient();
            BillingClient.Builder builder=  BillingClient.newBuilder(applicationContext);
            builder.setListener(new PurchaseActivityListener());
            builder.enablePendingPurchases();
            billingClient = builder.build();
        }

        LogUtil.info("Initializing the Billing Client");

    }
    catch (Exception ex)
    {
        LogUtil.error("Error while initializing billing client", ex);
    }

}

public static ApplicationBillingClient getInstance(Context applicationContext)
{
    if(isInitialized() == false)
    {
        initialize(applicationContext);
    }

    return  applicationBillingClient;
}

public void startConnection()
{
   billingClient.startConnection(new StateListener());
}

public boolean isReady()
{
    return billingClient.isReady();
}

public void getMonthlySubscription()
{
    try
    {
        if(billingClient.isReady())
        {
            SkuDetailsParams.Builder skuBuilder = SkuDetailsParams.newBuilder();
            skuBuilder.setType(BillingClient.SkuType.SUBS);
            skuBuilder.setSkusList(Arrays.asList(new String[]{MONTHLY_BILLING_SUBSCRIPTION_SKU}));
            SkuDetailsParams params = skuBuilder.build();
            billingClient.querySkuDetailsAsync(params, new SkuDetailsListener());
        }

    }
        catch (Exception ex)
    {
        LogUtil.error("Error while querying async SKU for Monthly Subscription", ex);
    }
  }
 }

 //In your activity
 ApplicationBillingClient appBillingClient = 
 ApplicationBillingClient.getInstance(applicationContext);
        if (appBillingClient.isReady() == false)
        {
            appBillingClient.startConnection();
        }
        else
        {
           appBillingClient.getMonthlySubscription();
        }