How to dynamically get all Google Gmail account ID in android app on button click.
Getting primary email id from android apps is one of the most important security feature for all the developers because with this they can actually be managed their all users using their email Id’s and also they know who installed their apps and right now using it. In this tutorial we have to use two type of permissions GET_ACCOUNTS and READ_PHONE_STATE . They both are the dangerous permissions and from the Android Marshmallow 6.0 they only can be used as runtime permissions. So user can himself decide to allow it or not. So here is the complete step by step tutorial for Get Android Device Primary Registered Email Address Programmatically.
How to Get Android Device Primary Registered Email Address Programmatically.
Code for MainActivity.java file.
package com.android_examples.getgoogleaccount_android_examplescom; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Patterns; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.regex.Pattern; import static android.Manifest.permission.GET_ACCOUNTS; import static android.Manifest.permission.READ_PHONE_STATE; import android.accounts.Account; import android.accounts.AccountManager; public class MainActivity extends AppCompatActivity { public static final int RequestPermissionCode = 1; ListView listView ; Button button; ArrayList<String> SampleArrayList ; ArrayAdapter<String> arrayAdapter; Pattern pattern; Account[] account ; String[] StringArray; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EnableRuntimePermission(); listView = (ListView) findViewById(R.id.listview1); button = (Button)findViewById(R.id.button); SampleArrayList = new ArrayList<String>(); pattern = Patterns.EMAIL_ADDRESS; GetAccountsName(); //Converting account string array list into string array. StringArray = SampleArrayList.toArray(new String[SampleArrayList.size()]); //Setting up string array into array adapter. arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, StringArray); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Adding array adapter into listview. listView.setAdapter(arrayAdapter); } }); } public void GetAccountsName(){ try { account = AccountManager.get(MainActivity.this).getAccounts(); } catch (SecurityException e) { } for (Account TempAccount : account) { if (pattern.matcher(TempAccount.name).matches()) { SampleArrayList.add(TempAccount.name) ; } } } public void EnableRuntimePermission() { ActivityCompat.requestPermissions(MainActivity.this, new String[] { GET_ACCOUNTS, READ_PHONE_STATE }, RequestPermissionCode); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case RequestPermissionCode: if (grantResults.length > 0) { boolean GetAccountPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean ReadPhoneStatePermission = grantResults[1] == PackageManager.PERMISSION_GRANTED; if (GetAccountPermission && ReadPhoneStatePermission) { Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show(); } } break; } } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.getgoogleaccount_android_examplescom.MainActivity" android:background="#FFF8E1"> <Button android:text="Click here to get all primary gmail email id's of android device programmatically" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:id="@+id/listview1"/> </RelativeLayout>
Code for MainActivity.java file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android_examples.getgoogleaccount_android_examplescom"> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Screenshots: