InAppBilling 活动和 finish()

发布于 2024-10-27 19:07:26 字数 4776 浏览 0 评论 0原文

我在集成谷歌的应用程序内结算时遇到很多麻烦。

我有一个应该进行应用内计费的活动。我使用 startActivity 方法从我的主要活动中调用此活动。



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

并调用 mBillingService.requestPurchase("android.test.purchased", "10") 在应用内结算活动的 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");
        }
    }

由于应用程序内计费对服务器进行异步调用,因此我应该何时调用 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.:我的老问题,仍未解决......

I'm having lots of trouble with integrating google's In-App-Billing.

I have an activity which is supposed to do the In-App-Billing. I'm calling this activity from my main activity using the startActivity method.



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

and calling mBillingService.requestPurchase("android.test.purchased", "10")
in the onCreate method on In-App-Billing activity.



 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");
        }
    }

Since In-App-Billing makes an asynchronous call to the server, when should i call finish() to return to the main activity. But i dont want the activity to end, i still need the results of the async call made. So how do i go about handling that.

I called the finish() after sending the request to the server. But then i got this exception :


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)

It didn't exit the application, but it stopped the InAppBillingActivity.

Any help is most appreciated.

Thanks,
AkasH

P.S.: My Old Question, still unsolved.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-11-03 19:07:27

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

这样你就可以开始计费,让它执行异步回调,当它收到“成功”或“失败”时,你可以调用 finish();发送回结果。

应用内结算的另一个教程是:简单的应用内结算 ,这为您提供了第二个视角,可能会帮助您了解 BillingService 试图实现的目标。

编辑

对于“Simple InApp Billin”的错误:
您在购买时遇到错误,您可以在返回 false 的计费服务中捕获此错误,否则在计费助手中您可以更新方法来阻止此错误:

 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()?");
            }
    }

You should use startActivityForResult()
Context.StartActivityforResult URL

That way you can start your billing, let it do it's Async callbacks and when it has received the 'success' or 'fail' you can call finish(); sending back the result.

Another Tutorial for In App Billing is : Simple InApp Billing , this gives you a second perspective and might help you come to terms with what the BillingService is trying to achieve.

EDIT

For your error with 'Simple InApp Billin':
You are getting an error in your purchase, you could catch this in the Billing Service where it is returning false, otherwise in the Billing Helper you can update the method to stop this:

 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()?");
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文