InAppBilling activity and finish ()

I have many problems integrating Google In-App-Billing.

I have an activity that In-App-Billing should do. I call this action from my main action 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 for 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 I have to call finish () to return to the main action. But I do not want the activity to end, I still need the results of an asynchronous call. So how can I handle this.

I called the finish line () after sending a 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) 

He did not exit the application, but stopped InAppBillingActivity.

Any help is most appreciated.

Thanks Akash

PS: My old question is still not resolved .....

+4
source share
1 answer

You must use startActivityForResult() Context .StartActivityforResult URL

This way you can start your billing, let it do asynchronous callbacks, and when it gets โ€œsuccessโ€ or โ€œfailโ€, you can call finish (); sending the result.

Another application billing tutorial: Simple InApp Billing , it gives you a second perspective and can help you agree on what BillingService is trying to achieve.

EDIT

For your mistake with "Simple InApp Billin": You get an error when buying, you can catch it in the Billing Service, where it returns false, otherwise in the Billing Assistant 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()?"); } } 
+1
source

Source: https://habr.com/ru/post/1346012/


All Articles