0

I am trying to create a register and login system using google account as username and I was wondering is there a way to get all google accounts on the android device as string and put them all in an array?

Programmer
  • 121,791
  • 22
  • 236
  • 328
Armin
  • 576
  • 6
  • 13
  • the repply is very extended and so much diferent parts. You need to start checking the Google api of gmail and calling a restservice from Unity3d. But the answer is yes, there a way to do it. – joreldraw Aug 21 '17 at 13:24

1 Answers1

1

This answer is based on my other answer which is in Java. By using AndroidJavaClass and AndroidJavaObject, you won't need a Java code and a jar/aar plugin at-all. It will all be done from C#.

string[] getAndroidEmailAccounts()
{
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    AndroidJavaClass ACManager = new AndroidJavaClass("android.accounts.AccountManager");
    AndroidJavaObject ACManagerAct = ACManager.CallStatic<AndroidJavaObject>("get", currentActivity);

    AndroidJavaObject accounts = ACManagerAct.Call<AndroidJavaObject>("getAccountsByType", "com.google");
    AndroidJavaObject[] accountArray = AndroidJNIHelper.ConvertFromJNIArray<AndroidJavaObject[]>(accounts.GetRawObject());

    string[] accountName = new string[accountArray.Length];

    for (int i = 0; i < accountName.Length; i++)
    {
        accountName[i] = accountArray[i].Get<string>("name");
    }

    return accountName;
}

You need this permission in your manifest:

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

This post describes how to use Manifest in Unity project.

Programmer
  • 121,791
  • 22
  • 236
  • 328