How to check if user has signed in Google Play account on the device before calling Google Billing functions

Viewed 181

I am trying to fetch the price for our from Google Play Store for the in-app purchase by calling https://developer.android.com/reference/com/android/billingclient/api/BillingClient#queryskudetailsasync

However, if the user has no Google account signed in, (or required to sign in again in Google Play app), the request will fail, but the billing response code will be BillingResponseCode.SERVICE_DISCONNECTED (which I cannot differentiate between Google Play service is actually failing on the device, or they simply just do not log in. I want to provide user clearer instruction to help them solve on the issue)

In our legacy code we have

        if (ContextCompat.checkSelfPermission(getContext(), permission.GET_ACCOUNTS)
                != PackageManager.PERMISSION_GRANTED) {
            // We have no way of knowing whether the user is logged into a google account so
            // default to yes
            return true;
        }

        Account[] list = mAccountManager.getAccounts();

        for (Account account : list) {
            if (account.type.equalsIgnoreCase(GOOGLE_ACCOUNT_TYPE)) {
                return true;
            }
        }

        return false;

However, it is constantly failing on the permission part now, even though if I remove the permission check, the rest of the logic works... (while the checkSelfPermission is returning PERMISSION_DENIED...)

I find another alternative suggestion, which is using GoogleSignIn, but this is an auth Api, so I wonder if there is any side effect

return GoogleSignIn.getLastSignedInAccount(context) != null
0 Answers
Related