how to implement Android In App BillingClient in Xamarin.Android Asynchronously

故事扮演 提交于 2019-12-11 18:47:14

问题


I am trying to implement below java code in c# referring to Android documentation

List<String> skuList = new ArrayList<> ();
skuList.add("premium_upgrade");
skuList.add("gas");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
    new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(BillingResult billingResult,
                List<SkuDetails> skuDetailsList) {
            // Process the result.
        }
    });

I have here 2 questions. I thought that i would run this code on a separate thread than UI thread like below to keep my ui responsive while network connection is done. is that the correct approach? QuerySkuDetailsAsync is called async but doesnt implement as async. how should this be working and how to handle in c# because it will fire and forget but Listener to handle the response.

public async Task<List<InAppBillingProduct>> GetProductsAsync(List<string> ProductIds)
        {
 var getSkuDetailsTask = Task.Factory.StartNew(() =>
            {

                var prms = SkuDetailsParams.NewBuilder();
                var type =   BillingClient.SkuType.Inapp;
                prms.SetSkusList(ProductIds).SetType(type);

                BillingClient.QuerySkuDetailsAsync(prms.Build(), new SkuDetailsResponseListener());

                return InAppBillingProducts;
            });
     return await getSkuDetailsTask;
        }

2nd question regarding how to handle with the listener as below. How do I return value from the listener. I need return list of InAppBillingProduct object.

 public class SkuDetailsResponseListener : Java.Lang.Object, ISkuDetailsResponseListener
    {
        public void OnSkuDetailsResponse(BillingResult billingResult, IList<SkuDetails> skus)
        {
             if (billingResult.ResponseCode == BillingResponseCode.Ok)
            {
                   // get list of Products here and return
            }
        }
    }

来源:https://stackoverflow.com/questions/58272917/how-to-implement-android-in-app-billingclient-in-xamarin-android-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!