1. Are you ready for the Galaxy S20? Here is everything we know so far!

Async Function Needs Context

Discussion in 'Android Development' started by RewindJAA, Sep 16, 2020.

  1. RewindJAA

    RewindJAA Newbie
    Thread Starter

    [Edit: I think I am asking, how do you access the calling Context in an Async callback function that has just got the Sku details?]

    I have an async callback function within another function (onSkuDetailsResponse)

    Code (Text):
    1.  
    2. public void querySkuDetailsAsync(@BillingClient.SkuType final String itemType, final List<String> skuList){
    3.     // Query the purchase async
    4.     SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    5.     params.setSkusList(skuList).setType(itemType);
    6.     mBillingClient.querySkuDetailsAsync(params.build(),
    7.                                                                        new SkuDetailsResponseListener() {
    8.                @Override
    9.                public void onSkuDetailsResponse(BillingResult response,  List<SkuDetails> skuDetailsList) {
    10.  
    Note 'public void onSkuDetailsResponse' is the async function in question.

    Within it I cycle through the Skus:

    Code (Text):
    1.  
    2. for (SkuDetails skuDetails : skuDetailsList) {
    3.     String Sku, Title, Description, Price;
    4.     Sku = skuDetails.getSku();
    5.     Title = skuDetails.getTitle();
    6.     Description = skuDetails.getDescription();
    7.     Price = skuDetails.getPrice();
    8.     MyIAPProduct.setSkuDetails(productsDetailsARR, getApplicationContext(), Sku, Title, Description, Price);
    9. }
    10.  
    NOTE: The function `MyIAPProduct.setSkuDetails` saves the Sku's detail to preferences.

    If I put 'this' where getApplicationContext() is, it turns red as 'this' at that point in time is the SkuDetailsReponseListener and not the Context within which this function is written.

    So I put 'getApplicationContext()' as shown in the code, because I thought that would be the right thing to do.

    However, when 'MyIAPProduct.setSkuDetails' runs (see code)

    Code (Text):
    1.  
    2. public static void setSkuDetails(ArrayList<MyIAPProduct> arr, Context cnt,
    3.                                  String Sku, String Title, String Description, String Price)
    4. {
    5.     for(MyIAPProduct prod: arr){
    6.         if(Sku.equals(prod.m_Sku)) {
    7.             prod.setSkuDetails(cnt, Title, Description, Price);
    8.         }
    9.     }
    10. }
    11.  
    and 'setSkuDetails' for the individual object is run:

    Code (Text):
    1.  
    2. public void setSkuDetails(Context cnt, String Title, String Description, String Price)
    3. {
    4.     m_Title = Title;
    5.     m_Description = Description;
    6.     m_Price = Price;
    7.     saveToPreferences(cnt);
    8. }
    9.  
    Then when it runs the function 'saveToPreferences' for the individual object:

    Code (Text):
    1.  
    2. public void saveToPreferences(Context cnt){
    3.     SharedPreferences sharedPref = ((Activity) cnt).getPreferences(Context.MODE_PRIVATE);
    4.     SharedPreferences.Editor editor = sharedPref.edit();
    5.     editor.putBoolean(m_Sku + "Purchased", m_Purchased);
    6.     editor.putString(m_Sku + "Title", m_Title);
    7.     editor.putString(m_Sku + "Description", m_Description);
    8.     editor.putString(m_Sku + "Price", m_Price);
    9.     editor.commit();
    10. }
    11.  
    the program crashed on the line

    Code (Text):
    1.  
    2.     SharedPreferences sharedPref = ((Activity) cnt).getPreferences(Context.MODE_PRIVATE);
    3.  
    where the Context is first used.

    Can anyone explain why?

    Can anyone explain what I need to do to get it working as I hope?
     


    #1 RewindJAA, Sep 16, 2020
    Last edited: Sep 16, 2020
Loading...

Share This Page

Loading...