Use a Gmail account to log in to my app?

For the Android application I'm working on, I need to create a profile to send and receive content to other users. For the login process, can I log in with the Gmail account associated with the phone? This will be the same account that is active for using the Google game.

This will make the whole login process very smooth, and I think that would be the best scenario.

Many thanks!

+3
source share
1 answer

Yes, it is possible and it is quiet. Before you begin, you need to add one permission for AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

, . :

Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");

google . , , , , :

public static String[] getAccounts(Context context) {
    Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
    String[] names = new String[accounts.length];

    for (int i = 0; i < accounts.length; ++i) names[i] = accounts[i].name;

    return names;
}

gmail ( Google) .

, "" Google , . :

<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>

Google. ().

- Google, . :

public static String getToken(Activity activity, String serviceName) {
        try {
            Bundle result = AccountManager.get(activity).getAuthToken(account, serviceName, null, activity, null, null).getResult();

            return result.getString(AccountManager.KEY_AUTHTOKEN);
        } catch (OperationCanceledException e) {
            Log.d("Test", "Operation Canceled");
        } catch (AuthenticatorException e) {
            Log.d("Test", "Authenticator Exception");
        } catch (IOException e) {
            Log.d("Test", "Auth IOException");
        }

        return null;
}

, API HTTP, , : -)

0

All Articles