Today we are going to learn how to fetch all registered email accounts from our device like google, facebook, etc. For fetching email accounts details we are going to use AccountManager.
AccountManager class provides access to a centralised registry of the user’s online accounts. The user enters credentials (username and password) once per account, granting applications access to online resources with “one-click” approval.
1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.
2. First create a xml layout naming activity_main.xml and add the following code. In this layout we are going to display registered email accounts on TextView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:padding="10dp" android:text="Google Accounts" android:textColor="#FF1744" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/googleAccounts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:gravity="left" android:padding="10dp" android:textColor="#424242" android:textSize="15sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:gravity="center" android:padding="10dp" android:text="All Accounts" android:textColor="#FF1744" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/allAccounts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:gravity="left" android:padding="10dp" android:textColor="#424242" android:textSize="15sp" /> </LinearLayout> </ScrollView> |
3. Now come to your MainActivity.java and add the following code. In this code first we are fetching google accounts and all registered accounts as well. By using AccountManager we can fetch account type so by fetching account type we can fetch account details related to that particular account like we fetched google account details by using com.google account type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.getaccounts_demo; import android.accounts.Account; import android.accounts.AccountManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private static TextView googleAccounts, allAccounts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); googleAccounts = (TextView) findViewById(R.id.googleAccounts); allAccounts = (TextView) findViewById(R.id.allAccounts); //Get all Google Accounts try { String googleAccount = ""; Account[] accounts = AccountManager.get(MainActivity.this).getAccountsByType("com.google");//Use com.google //Loop all accounts for (Account account : accounts) { System.out.println("Google Account Name - " + account.name + " : Account Type - " + account.type + "n");//Get account name and type googleAccount += account.name + "n"; } googleAccounts.setText(googleAccount);//Set Text } catch (Exception e) { Log.i("Exception", "Exception:" + e); } //Get All registered accounts on device try { String allRegisteredAccounts = ""; Account[] accounts = AccountManager.get(MainActivity.this).getAccounts();//Get All accounts //Loop to all accounts for (Account account : accounts) allRegisteredAccounts += " Account Name - " + account.name + "nAccount Type - " + account.type + "nn";//get Account name and type allAccounts.setText(allRegisteredAccounts);//set text and type } catch (Exception e) { Log.i("Exception", "Exception:" + e); } } } |
4. Now, you are all done, you can use above code to fetch account details according to your requirements.
Thanks.
Subscribe to us and get the latest news.