I am trying to write an Android Plugin for Unity3D to interface with the Google Play In App Billing. I am aware that there are existing plugins out there for this, but I wish to do it on my own.
It appears that I need to catch Android's Activity::onActivityResult to handle a successful purchase through the GPlay IAB SDK. My issue is that my Java class does not contain an Activity because I want it to run in the background behind the actual Unity application.
This is the code from the GPlay IAB SDK which initiates the Purchase flow. If I pass "UnityPlayer.currentActivity" as the activity, Google Play pops up, and you can successfully purchase the product. However, you do not receive a successful message to the OnIabPurchaseFinishedListener. If the purchases is unsuccessful (ie: you already own the product) then I do receive the callback there.
public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
OnIabPurchaseFinishedListener listener, String extraData)
...
...
act.startIntentSenderForResult(pendingIntent.getIntentSender(),
requestCode, new Intent(),
Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
Full Source Here: http://pastebin.com/xwUbrwTz
And here is the section from the Google Play In App Billing SDK's (example code) which posts the successful callback (which I am not receiving)
/**
* Handles an activity result that's part of the purchase flow in in-app billing. If you
* are calling {@link #launchPurchaseFlow}, then you must call this method from your
* Activity's {@link android.app.Activity@onActivityResult} method. This method
* MUST be called from the UI thread of the Activity.
*
* @param requestCode The requestCode as you received it.
* @param resultCode The resultCode as you received it.
* @param data The data (Intent) as you received it.
* @return Returns true if the result was related to a purchase flow and was handled;
* false if the result was not related to a purchase, in which case you should
* handle it normally.
*/
public boolean handleActivityResult(int requestCode, int resultCode, Intent data)
Full Source: http://pastebin.com/VTfxJhKx
Here is how Google's example code processes the onActivityResult callback:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
Now, the reason I don't want to override the UnityPlayerActivity class (Search "Extending the UnityPlayerActivity Java Code" on Google, it's the second link. Lack of reputation prevents me from posting the direct link.) is because this requires you to modify the androidmanifest.xml to point to the new "Launcher" - this is a problem because several of the existing Ad platforms for Android already require you to modify the launcher to point to their own Java class. I would like to be able to co-exist with these which prevents me from extending the existing Unity Activity.
I attempted to start my own Activity within my Java class (which is spawned by Unity without issues) but that minimizes the Unity application and pulls up a blank Activity - obviously not what I want.
My question(s): Can I extend/catch/hook into the existing UnityAndroidPlayer class and add an onActivityResult function (it doesn't have one by default).
If no, can I make an activity within an Android plugin that doesn't take focus on the phone?
If no, can I modify the code from the Google Play SDK (act.startIntentSenderForResult(..)) to notify me in a different way?
If no, what can I do?