Android Tutorial Android InApp Purchase Tutorial + Source Code

ilan

Expert
Licensed User
Longtime User
A question: how do I do if my app a month or a year to enable the full version?
Should I use the "Managed Product" or "subscription"?

you can use also a "Managed" product (maybe it is the better solution)

and as soon as you call manager.ConsumeProduct the user will be able to purchase that product again.
like this you can avoid mistakes by the user so he will not purchase the same product twice.

so after he purchase the "Managed" product you store the date of purchase (in any way you like, example create a file with purchased date)
then after the purchased period is over (like a month or year) you consume that product with manager.ConsumeProduct and the user will be able to purchase it again.
 

sorex

Expert
Licensed User
Longtime User
Ilan,

I think this line

B4X:
 If p.ProductId = "remove_ads" Then 'check if item NoAds was purchased

needs to be

B4X:
 If p.ProductId = "remove_ads" And p.PurchaseState=p.STATE_PURCHASED Then 'check if item NoAds was purchased

otherwise it will remove ads when it returns purchases that are cancelled or refunded
(according to the dox they will still be listed as purchase but with code 1 (canceled) or 2 (refunded) instead of 0 (purchased))
 

ilan

Expert
Licensed User
Longtime User
otherwise it will remove ads when it returns purchases that are cancelled or refunded

Purchases that were refunded or cancelled should not appear in purchases.values. Only purchases that has purchase.state = true should appear in purchases so you ask the same twice.
 

sorex

Expert
Licensed User
Longtime User

ilan

Expert
Licensed User
Longtime User
I understand but what i mean is only purchased item with state 0 should be included it purchases.values (map)

Otherwise you are right. @Erel should tell us if all purchases including canceled and refunded are included in purchases.values or only purchases with state 0.
 

Scantech

Well-Known Member
Licensed User
Longtime User
Why is the File.Exists(File.DirInternal,"ad.txt") = False and allows the ad to be disabled and removes button?
B4X:
    If FirstTime Then
        manager.Initialize("manager", inappkey)
        manager.DebugLogging = True
    Else
        If File.Exists(File.DirInternal,"ad.txt") = False Then 'manager will be called only on firsttime true, if firstime is false we will check if ad.txt exists what means that the app was purchased. now we should hide the "buy me" button and disable ads until we will be able to check again if the "no_ads" item was purchased!
            'hide purchase button
            'disable ads
        End If   
    End If

    If File.Exists(File.DirInternal,"ad.txt") Then adtimer.Enabled = False 'change default ad support to off

Why can't it be like this. If the app is paid and i open the app for first time or not, i want to disable and remove purchase button. I did not see anywhere in the example the button is hidden when first time is false? I think your Example file is not updated?
B4X:
    If FirstTime Then
        manager.Initialize("manager", inappkey)
        manager.DebugLogging = True
    End If

    If File.Exists(File.DirInternal,"ad.txt") Then
        'hide purchase button
        adtimer.Enabled = False 'change default ad support to off
    End If
 
Last edited:

ilan

Expert
Licensed User
Longtime User

only when a purchase was done i create the ad.txt file.

the manager will be intialized only if FIrsttime = true, and firsttime = true wont be always the case like if you exit your app via home button and then return only "Activity_resume" will be called (firsttime = false) and if you exit your app via back key and return activity_create will be called and then activity_resume (firsstime = false) will be called.

so because we only intialize the manager when firsstime = true we need to store the fact that the user has purchased our product some how.
what you could do is to use the starter service and intialize manager in it and store the purchase data on a booleon and on every activity_resume ask if that variable in the starter service is true or you can do it by creating a file and ask if that file exists.

i am sure that there are more ways to do it, it is up to you what you prefer.

about the purchase button, in this example i have not hided the purchase button but you can hide it easily when you ask if the ad.txt file exists.
 

nhz

Member
Licensed User
Longtime User
Hi, do we need to Publish the empty app first or just upload it will do?
 

aeric

Expert
Licensed User
Longtime User
the search function in this forum is not good, i almost never find threads that i remember i have read before
+1 After I spent some time read the tutorial by Erel, after some trial and error, successfully publish the demo app to Play store, now I found this thread.

Edit: I think because your title is "Android InApp Purchase" but I searched for "In app billing"
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…