[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)
Note 'public void onSkuDetailsResponse' is the async function in question.
Within it I cycle through the Skus:
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)
and 'setSkuDetails' for the individual object is run:
Then when it runs the function 'saveToPreferences' for the individual object:
the program crashed on the line
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?
I have an async callback function within another function (onSkuDetailsResponse)
Code:
public void querySkuDetailsAsync(@BillingClient.SkuType final String itemType, final List<String> skuList){
// Query the purchase async
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(itemType);
mBillingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult response, List<SkuDetails> skuDetailsList) {
Note 'public void onSkuDetailsResponse' is the async function in question.
Within it I cycle through the Skus:
Code:
for (SkuDetails skuDetails : skuDetailsList) {
String Sku, Title, Description, Price;
Sku = skuDetails.getSku();
Title = skuDetails.getTitle();
Description = skuDetails.getDescription();
Price = skuDetails.getPrice();
MyIAPProduct.setSkuDetails(productsDetailsARR, getApplicationContext(), Sku, Title, Description, Price);
}
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:
public static void setSkuDetails(ArrayList<MyIAPProduct> arr, Context cnt,
String Sku, String Title, String Description, String Price)
{
for(MyIAPProduct prod: arr){
if(Sku.equals(prod.m_Sku)) {
prod.setSkuDetails(cnt, Title, Description, Price);
}
}
}
and 'setSkuDetails' for the individual object is run:
Code:
public void setSkuDetails(Context cnt, String Title, String Description, String Price)
{
m_Title = Title;
m_Description = Description;
m_Price = Price;
saveToPreferences(cnt);
}
Then when it runs the function 'saveToPreferences' for the individual object:
Code:
public void saveToPreferences(Context cnt){
SharedPreferences sharedPref = ((Activity) cnt).getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(m_Sku + "Purchased", m_Purchased);
editor.putString(m_Sku + "Title", m_Title);
editor.putString(m_Sku + "Description", m_Description);
editor.putString(m_Sku + "Price", m_Price);
editor.commit();
}
the program crashed on the line
Code:
SharedPreferences sharedPref = ((Activity) cnt).getPreferences(Context.MODE_PRIVATE);
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?
Last edited: