InAppBilling activity and finish()
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(354开发者_JS百科9): 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.....
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()?");
}
}
精彩评论