I'm developing an application where I need to see the list of accounts in a user's phone. I have go this code and it seems to work perfectly. The only problem I'm facing is I want to get list of accounts and not just one so how will this be achieved? I'm quite sure that a listview is required but I didn't get any clue as to how can I use that in here.
The code:
package com.example.root.accountspermission;
import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Patterns;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
public static final int PERMS_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
}
} else {
//do some stuff
String email = null;
Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (gmailPattern.matcher(account.name).matches()) {
email = account.name;
}
}
TextView viewEmail = (TextView) findViewById(R.id.email_address_view);
viewEmail.setText("Email From Device: " + email);
Toast.makeText(this, "Android Device Registered Email Address: " + email, Toast.LENGTH_LONG).show();
}
}
}
Any kind of help would be appreciated.
UPDATE:
Thank you for your answer 'AndiGeeky'. I was able to modify the code a bit, have a look:
import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Patterns;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private ListView lv;
public static final int PERMS_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
}
} else {
//do some stuff
lv = (ListView) findViewById(R.id.accountsList);
List<String> emails = new ArrayList<String>();
Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (gmailPattern.matcher(account.name).matches()) {
emails.add(account.name);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,emails);
lv.setAdapter(adapter);
//TextView viewEmail = (TextView) findViewById(R.id.email_address_view);
//viewEmail.setText("Email From Device: " + emails.size());
Toast.makeText(this, "Android Device Registered Email Address: " + emails.size(), Toast.LENGTH_LONG).show();
}
}
}
I was able to print all the accounts in a listview.
Code for XML:
<ListView
android:id="@+id/accountsList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
You can also view the users on an Android device by going to Settings and then searching “users”. You should get a result for a users section where you can see a listing of current users as well as options to create/delete new users. Users option within the Settings of an Android.
Under the Personal tab in Settings, you'll find an option for Accounts. This is separate from the tab, where you'll find a lot of preferences related to how your account operates on your device.
android.accounts.AccountManager. This class provides access to a centralized 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.
Call getAccounts() method from AccountManager class to get all registered email accounts on android phone.
Change your implementation as below:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
}
} else {
//do some stuff
ArrayList<String> emails = new ArrayList<>();
Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (gmailPattern.matcher(account.name).matches()) {
emails.add(account.name);
}
}
TextView viewEmail = (TextView) findViewById(R.id.email_address_view);
viewEmail.setText("Email From Device: " + emails.size());
Toast.makeText(this, "Android Device Registered Email Address: " + emails.get(0), Toast
.LENGTH_LONG).show();
}
}
}
Your Code:
Here your code just looping through the user accounts and when it follows pattern mentioned just store that account to string
and leave other accounts!
Edited Code
I have just stored all user accounts which follows pattern mentioned in code in a list.
Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With