Using billing in an application in the Application class

In my application, I have one product in the application that you can buy. This is an excellent service unlocker for the application. Is it wrong to check the purchase status in a method onCreatein a class that extends Application?

The advantage is that this can be done in the background and is not tied to Activity. I currently have a screensaver, and after that my second action begins, which checks the status of the purchase. It starts in AsyncTask, which may take some time. Because of this, the user can see that the premium purchase button is blinking.

I could do this in the Activity splash screen, but then I would not have access to the connection, and I cannot associate the purchase action with the button in the main operation.

That way, I can also store the boolean value of the purchase status in the class Application. I need to access them from different places. I am currently passing value as Intentextra for actions requiring it.

The only drawback to this is that the compound will be alive for a long time. In my main activity, I bind a compound in a method onCreateand untie it in onDestroy. Does the active connection open the active connection, or is it normal to open the connection in the class Application?

Also, if the connection is opened in a class that extends Application, I do not have access to any destroy method to close the connection. According to the documentation , onTerminatea class is Applicationnever called so that user code is executed.

It will never be called on an Android production device, where processes are deleted just by killing them; no user code (including this callback) is executed at the same time.

In my main activity I am currently

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 onClickListenerbuy buttons

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);
}
+4
source share

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


All Articles