我正在尝试为 Unity3D 编写一个 Android 插件来与 Google Play In App Billing 交互。我知道有现有的插件可以解决这个问题,但我希望自己做。
看来我需要捕获 Android 的 Activity::onActivityResult 才能通过 GPlay IAB SDK 成功购买。我的问题是我的 Java 类不包含 Activity,因为我希望它在实际 Unity 应用程序后面的后台运行。
这是 GPlay IAB SDK 中启动购买流程的代码。如果我将“UnityPlayer.currentActivity”作为活动传递,则弹出Google Play,您可以成功购买该产品。但是,您不会收到到 OnIabPurchaseFinishedListener 的成功消息。如果购买不成功(即:您已经拥有该产品),那么我会在那里收到回调。
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));
完整来源:http: //pastebin.com/xwUbrwTz
这是来自 Google Play In App Billing SDK 的部分(示例代码),它发布了成功的回调(我没有收到)
/**
* 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)
完整来源: http: //pastebin.com/VTfxJhKx
以下是 Google 的示例代码如何处理 onActivityResult 回调:
@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.");
}
}
现在,我不想覆盖 UnityPlayerActivity 类的原因(在 Google 上搜索“扩展 UnityPlayerActivity Java 代码”,这是第二个链接。缺乏声誉使我无法发布直接链接。)是因为这需要您修改androidmanifest.xml 指向新的“启动器”——这是一个问题,因为一些现有的 Android 广告平台已经要求您修改启动器以指向它们自己的 Java 类。我希望能够与这些共存,这会阻止我扩展现有的 Unity Activity。
我试图在我的 Java 类中启动我自己的 Activity(它是由 Unity 毫无问题地生成的),但这会最小化 Unity 应用程序并拉出一个空白 Activity——显然不是我想要的。
我的问题:我可以扩展/捕获/挂钩到现有的 UnityAndroidPlayer 类并添加一个 onActivityResult 函数(默认情况下它没有)。
如果不是,我可以在不关注手机的 Android 插件中进行活动吗?
如果不是,我可以修改 Google Play SDK (act.startIntentSenderForResult(..)) 中的代码,以不同的方式通知我吗?
如果没有,我该怎么办?