Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Let's write the code to add Google Play In-App Purchases to our game. Note that most of the code we'll be looking at I actually copied and pasted from the Android Billing Library. If you open up the Dungeons folder, go into source, all the way down to all the code, and if you open up Dungeons.java, Application.java, that's where I got the sample code. So any methods that I reference here, you'll find that in there, which should be especially helpful for those of you who don't have access to the Exercise Files. So let's go into Eclipse, and first I'm going to point out that I have imported all these robotmedia classes.
So you will need to do that, and then scroll down, I created a property that's an AbstractBillingObserver called mBillingObserver, scroll down into onCreate and I set BillingController to debug, and then I set the configuration for the BillingController. And here, you can copy and paste this ObfuscationSalt code, but this is a special key that should be customized in random from you. So you can go in and edit these numbers if you're going to publish your app. I just copied this from the Dungeons project, so I'm not worried about it being unique because I'm not going to publish the version that you're looking at right now.
Then, I have my Public Key, and this is where you're going to need to paste in that Public Key that you've got from your Android developer account. I set the mBillingObserver = new AbstractBillingObserver, and then I am just saying that for each of these methods, I'm going to call onBillingChecked, onPurchaseStateChanged, onRequestPurchaseResponse, onSubscriptionChecked. So you'll need to implement those methods inside of this class. Register the observer, check for billing support, check for subscription support, and then I've implemented onDestroy, and in onDestroy, I unregister the BillingObserver.
I'm going to scroll down, and in the buyItem method, I run BillingController, requestPurchase, passing in me, that's the instance of moleitx, and then I pass in this string. Now, if you haven't already uploaded your app and created In-App Purchase you can actually still test that it works by testing for the purchase android.test.purchased. Of course if you created an In-App Purchase for your app, you want to pass in that string here; android.test.purchased will also automatically return true.
So as long as that billing service is running, then it's going to work. And then you can see how I've implemented most of these other delegate methods. I just have a log that explains what happened, I tagged it with IAP, and onRequestPurchaseResponse, I check to see if the response is equal to ResponseCode.RESULT_OK. If so, I unlock the skin. You can also define what you do when you check the billing, and when you check the subscription status. So you could see, it's not super complicated, and if you want more detailed code, you can look at the Dungeons sample and see what else you can do and what other methods you could implement to check for your In-App Purchases and to restore them.
But once you have this code set up and you run it on a device that has the latest version of Google Play, you'll be able to make In-App Purchases through Google Play.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99334 Viewers
56 Video lessons · 112595 Viewers
71 Video lessons · 81415 Viewers
131 Video lessons · 39083 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.