In my application I have one in-app product that can be purchased. This is a premium service unlocker for the application. Is it bad to check the purchase status in onCreate
method in a class that extends Application
?
The pros of this is that it can be done in the background and is not tied to an Activity. Currently, I have a splash screen and after that my second Activity starts which checks the purchase status. It is run in an AsyncTask
which can take a while. Because of this, the user might see the premium version purchase button to flash.
I could do that in the splash screen Activity, but then I wouldn't have access to the connection and I can't bind the purchase action to a button in the main Activity.
This way I can also store the boolean value of the purchase state in the Application
class. I need to access that from multiple places. Currently I am passing the value as Intent
extra to the Activities that require it.
The only downside of this is that the connection would be alive for a long time. In my main Activity, I bind the connection in the onCreate
method and unbind it in onDestroy
. Does it keep an active connection open or is it okay to open the connection in the Application
class?
Also, if the connection is opened in the class that extends Application
, I have no access to any on destroy method to close the connection. According to the documentation, onTerminate
in the Application
class is never called so that user code is executed.
It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.
In my main Activity I currently have this
IInAppBillingService mService;
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
In onCreate
@Override
public void onCreate() {
super.onCreate();
bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConnection, Context.BIND_AUTO_CREATE);
In onDestroy
@Override
public void onDestroy() {
super.onDestroy();
if(mService != null) {
unbindService(mServiceConnection);
}
}
And in the onClickListener
of the purchase button
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), "proversion", "inapp", "...");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
if(pendingIntent != null) {
startIntentSenderForResult(pendingIntent.getIntentSender(), PURCHASE_RESULT_CODE, new Intent(), 0, 0, 0);
}