应用内购买遇到待处理意图和活动切换问题

4

好的,我已经尝试解决这个问题好几天了,我并不是在这里寻求别人替我完成工作,因为我一直在排除故障并修复LogCat中的每一个错误消息。我正在使用Andengine开发一个Android游戏(这可能是问题的一部分,熟悉它可能会有帮助)。我的游戏活动都是单场景的,没有任何物理学等高级内容,只有一堆精灵和纹理。我还使用Andengine为游戏中的所有其他活动设置了图形界面,因为我发现这是一种非常简单的方法来设置具有吸引力的屏幕。其中一个屏幕是我的应用内商店,用户可以购买关卡包和新的精灵。整个计费过程都很顺利,购买可以通过市场进行,没有什么太复杂的地方...

当用户点击购买按钮时,市场屏幕弹出并加载他们选择的产品(这些是真实产品,而不是Android测试,尽管游戏尚未发布)。无论我使用"Android 2.0"实现将其作为游戏栈的一部分还是使用"Android 1.6"实现将其作为自己的栈的一部分,市场屏幕都会弹出到当前活动上。我更喜欢使用Android 2.0实现,但如果我只能让1.6工作,我也会接受。总之,当用户使用返回按钮取消购买或使用信用卡完成购买时,问题就出现了,这两种情况都会导致市场屏幕消失,并且应用程序启动一个新的活动,只是一个黑屏(最终超时并导致强制关闭)。购买过程可以正常进行,但由于游戏在到达更改用户游戏道具的代码之前崩溃,因此用户无法获得产品。现在来看一些代码,我使用了这个教程(http://www.anddev.org/advanced-tutorials-f21/simple-inapp-billing-payment-t52060.html),并没有太多更改。BillingHelper类非常重要,因为它包含requestPurchase()方法和startBuyPageActivity()方法。我像这样从我的StoreFront活动调用请求购买:

            BillingHelper.requestPurchase(StoreFront.this, itemID); 

在 StoreFront 的 onCreate 中,我有这些东西(教程告诉我要这样做):

        startService(new Intent(mContext, BillingService.class));
    BillingHelper.setCompletedHandler(mTransactionHandler);

...

//some handler that billing needs
public Handler mTransactionHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        Log.i(TAG, "Transaction complete");
        Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
        Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

        if(BillingHelper.latestPurchase.isPurchased()){
            //TODO do something here if we've completed our latest purchase,
            //this should be with the status bar notifications and
            //saved preferences
        }
    };

};

所以我认为问题不在这里。以下是BillingHelper的相关部分。
protected static void requestPurchase(Context activityContext, String itemId){
    if (amIDead()) {
        return;
    }
    Log.i(TAG, "requestPurchase()");
    Bundle request = makeRequestBundle("REQUEST_PURCHASE");
    request.putString("ITEM_ID", itemId);
    try {
        Bundle response = mService.sendBillingRequest(request);

        //The RESPONSE_CODE key provides you with the status of the request
        Integer responseCodeIndex   = (Integer) response.get("RESPONSE_CODE");
        //The PURCHASE_INTENT key provides you with a PendingIntent, which you can use to launch the checkout UI
        PendingIntent pendingIntent = (PendingIntent) response.get("PURCHASE_INTENT");
        //The REQUEST_ID key provides you with a unique request identifier for the request
        Long requestIndentifier     = (Long) response.get("REQUEST_ID");
        Log.i(TAG, "current request is:" + requestIndentifier);
        C.ResponseCode responseCode = C.ResponseCode.valueOf(responseCodeIndex);
        Log.i(TAG, "REQUEST_PURCHASE Sync Response code: "+responseCode.toString());

        startBuyPageActivity(pendingIntent, new Intent(), activityContext);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed, internet error maybe", e);
        Log.e(TAG, "Billing supported: "+isBillingSupported());
    }
}

我曾经尝试从StoreFront中以各种参数(例如StoreFront.this,getApplicationContext(),存储在其他地方的静态上下文,存储在其他地方的静态Activity,getBaseContext()等)调用“ActivityContext”,但都未成功。

这是另一个相关的活动。

private static void startBuyPageActivity(PendingIntent pendingIntent, Intent intent, Context context){
    //android 1.6 method
    try {
        pendingIntent.send(context, 0, intent);         
    } catch (CanceledException e){
        Log.e(TAG, "startBuyPageActivity CanceledException");
    }
}

没有什么花哨的东西,我只希望用户在购买物品或在过程中按下返回键时能够返回到我的各种活动之一(最好是StoreFront)。请帮帮忙!

编辑:我希望任何可能的解决方案都能够允许应用内购买在购买完成后返回到我的应用程序,即使是最混乱的解决方案。

编辑

问题的日志和方法调用:

  "BillingService Starting", 
  BillingHelper.setCompletedHandler(), 
  StoreFront.onStart() called, 
  StoreFront.onResume() called, 
  "BillingService Service starting with onCreate", 
  "BillingService Market Billing Service Successfully Bound", 
  "BillingService Market Billing Service Connected", 
  BillingHelper.instantiateHelper(), 
  then this is where I actually click the buy button in the store (all of that runs just when opening StoreFront):
  BillingHelper.setCompletedHandler(), 
  BillingHelper.isBillingSupported(), 
  BillingHelper.amIDead(), 
  BillingHelper.makeRequestBundle(), 
  "BillingService isBillingSupported response was: RESULT OK", 
  BillingHelper.requestPurchase(), 
  BillingHelper.amIDead(), 
  "BillingService requestPurchase()", 
  BillingHelper.makeRequestBundle(), 
  "BillingService current request is ......", 
  "BillingService REQUEST PURCHASE Sync Response code: RESULT OK", 
  BillingHelper.startBuyPageActivity(), 
  "BillingService Recieved action: com.android.vending.billing.RESPONSE CODE", 
  "BillingService checkResponseCode got requestID..."
  "BillingService checkResponseCode go responseCode RESULT ERROR" 
  (this is because I can't purchase on this device), 
  and then I get an Error message saying: "E 32427 Surface surface (identity=5925) is invalid, err=-19 (No such device)" and from there nothing works anymore. 

另外,我已经在另一台手机上测试过了(与我合作的另一个开发人员可以在其中购买物品,但仍然会出现黑屏错误),他也没有收到您在评论中提到的 Handler 消息。

编辑:如果我必须猜测错误在哪里,我会说是这个:

06-16 11:20:23.635: DEBUG/dalvikvm(3807): GC_EXPLICIT freed 53K, 45% free 3710K/6663K, external 1K/513K, paused 102ms
06-16 11:20:23.885: ERROR/Surface(3807): surface (identity=158) is invalid, err=-19 (No such device)
06-16 11:20:23.905: ERROR/Surface(3807): surface (identity=158) is invalid, err=-19 (No such device)
06-16 11:20:23.905: ERROR/Surface(3807): surface (identity=158) is invalid, err=-19 (No such device)
06-16 11:20:23.905: ERROR/Adreno200-EGL(3807): egliSwapWindowSurface: unable to dequeue native buffer

请注意,中断异常是Andengine库所期望的,因此这是一个误导。
此外(希望这在这里是允许的),我将为解决方案提供PayPal奖励。如果这违反了SO的条款,请删除此行,但请不要关闭此问题。

1
你的处理程序是否返回“交易完成”和“已购买商品为:”?也就是说,处理程序是否被调用了? - Blundell
1
你从LogCat中获取了什么信息?InApp教程中有很多Log输出,因此你的LogCat可能能够显示它停止的位置。请查看BillingHelper.java文件中的verifyPurchase(string, string)方法,这可能是你目前的进展。 - Blundell
1
不要在评论中写这样的内容。只需编辑您的问题并在底部放置EDIT。现在阅读它。 - Blundell
1
从我所了解的情况来看,似乎您只是遇到了一些常见的计费错误。您能否检查一下这些常见问题:http://stackoverflow.com/questions/5501370/error-testing-in-app-billing-sample-dungeons/5501526#5501526 - Blundell
1
你有开发手机吗?你可以使用http://www.perfectomobile.com/,他们提供一个小时的免费试用。如果能够获得成功购买的logcat,就能看到它进行到哪里,然后我们就可以找出为什么没有调用处理程序。 - Blundell
显示剩余5条评论
1个回答

5
我可能知道出了什么问题,我有一个测试让你做。购买屏幕在用户取消或完成购买后运行结束调用。对于某些原因,我的结束调用一直漂移到当前运行的活动中并将其关闭。
以下是日志中相关的行:
06-16 11:20:22.774: WARN/ActivityManager(132): Duplicate finish request for HistoryRecord{40ace828 com.android.vending/.billing.InAppBuyPageActivity}
我认为我已经解决了这个代码问题,但问题是我不确定自己到底做了些什么(也许在我的购买完成处理程序中没有调用finish...)
我不了解Andgen,但如果主要Andgen活动上调用结束调用会发生什么?我想它将停止执行并可能导致黑屏和应用崩溃。
因此,为了测试这个问题,请为您的购买页面创建一个单独的活动。它不需要太复杂 - 可以在启动后只买一个罐装产品。运行您的代码并查看是否仍会给您带来毁灭性的黑屏。我打赌:它可能会退出活动返回到您的游戏,但我认为它会工作。
希望这能帮助你,祝好运!

2
你是我的个人英雄。赏金全归你,我非常感谢。 - Sam Stern
在创建新活动并从那里进行购买,然后在购买后返回到我的AndEngine活动之后,问题类似,现在一切都可以工作:D - Spider

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接