InAppBilling活动和完成()

时间:2011-03-31 10:10:35

标签: java android billing

我在整合谷歌的应用内结算方面遇到了很多麻烦。

我有一项应该进行应用内结算的活动。我正在使用startActivity方法从我的主要活动中调用此活动。



Intent i = new Intent();
i.setComponent(new ComponentName("com.mypackage.mainactivity","com.mypackage.mainactivity.InAppBillingActivity"));                
startActivity(i);

并致电mBillingService.requestPurchase("android.test.purchased", "10") 在In-App-Billing活动的onCreate方法中。



 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Handler();
        mInAppBillingPurchaseObserver = new InAppBillingPurchaseObserver(mHandler);
        mBillingService = new BillingService();
        mBillingService.setContext(this);

        ResponseHandler.register(mInAppBillingPurchaseObserver);
        boolean supported = mBillingService.checkBillingSupported();
        System.out.println("onCreate.isBillingSupported " + supported);
        if(supported) {
            Log.i("AJ", "Calling requestPurchase");
            mBillingService.requestPurchase("android.test.purchased", "10");
        }
    }

由于In-App-Billing对服务器进行异步调用,我何时应调用finish()返回主活动。但我不希望活动结束,我仍然需要异步调用的结果。那么我该如何处理呢。

我在向服务器发送请求后调用了finish()。但后来我得到了这个例外:


05-29 03:11:58.897: ERROR/ActivityThread(3549): Activity com.mypackage.mainactivity.InAppBillingActivity has leaked ServiceConnection com.mypackage.mainactivity.BillingService@45b0ad50 that was originally bound here
05-29 03:11:58.897: ERROR/ActivityThread(3549): android.app.ServiceConnectionLeaked: Activity com.mypackage.mainactivity.InAppBillingActivity has leaked ServiceConnection com.mypackage.mainactivity.BillingService@45b0ad50 that was originally bound here
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread$PackageInfo$ServiceDispatcher.(ActivityThread.java:1158)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1053)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ContextImpl.bindService(ContextImpl.java:908)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.mypackage.mainactivity.BillingService.bindToMarketBillingService(Unknown Source)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.mypackage.mainactivity.BillingService.access$000(Unknown Source)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.mypackage.mainactivity.BillingService$BillingRequest.runRequest(Unknown Source)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.mypackage.mainactivity.BillingService.checkBillingSupported(Unknown Source)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.mypackage.mainactivity.InAppBillingActivity.onCreate(Unknown Source)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread.access$2500(ActivityThread.java:129)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.os.Looper.loop(Looper.java:143)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at android.app.ActivityThread.main(ActivityThread.java:4701)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at java.lang.reflect.Method.invoke(Method.java:521)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-29 03:11:58.897: ERROR/ActivityThread(3549):     at dalvik.system.NativeStart.main(Native Method)

它没有退出应用程序,但它停止了InAppBillingActivity。

非常感谢任何帮助。

谢谢, AKASH

P.S。:My Old Question,尚未解决.....

1 个答案:

答案 0 :(得分:1)

你应该使用startActivityForResult() Context.StartActivityforResult URL

通过这种方式,您可以开始计费,让它执行异步回调,当它收到“成功”或“失败”时,您可以调用finish();发回结果。

In App Billing的另一个教程是:Simple InApp Billing,这为您提供了第二个视角,可能会帮助您了解BillingService尝试实现的目标。

修改

对于'Simple InApp Billin'的错误: 您在购买时收到错误,您可以在结算服务中发现错误,否则在结算助手中您可以更新方法以停止此操作:

 protected static void verifyPurchase(String signedData, String signature) {
            ArrayList<VerifiedPurchase> purchases = BillingSecurity.verifyPurchase(signedData, signature);
            if(purchases != null && !purchases.isEmpty()){ // Check for NPE
                    latestPurchase = purchases.get(0);

                    confirmTransaction(new String[]{latestPurchase.notificationId});
            } else {
                    Log.d(TAG, "BillingHelper.verifyPurchase error. purchases was null");
            }

            if(mCompletedHandler != null){
                    mCompletedHandler.sendEmptyMessage(0);
            } else {
                    Log.e(TAG, "verifyPurchase error. Handler not instantiated. Have you called setCompletedHandler()?");
            }
    }