我正在开发一个应用程序,我需要查看用户手机中的帐户列表。我已经执行了这段代码,它似乎工作得很好。我面临的唯一问题是我想要获取帐户列表而不仅仅是一个帐户,那么如何实现这一目标呢? 我很确定需要一个列表视图,但我没有得到任何关于如何在这里使用它的线索。
代码:
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();
}
}
}
任何形式的帮助将不胜感激。
更新:
感谢您的回答“AndiGeeky”。我可以稍微修改一下代码,看看:
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();
}
}
}
我能够在列表视图中打印所有帐户。
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" />
更改您的实现如下:
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();
}
}
}
您的代码:
这里您的代码只是循环访问用户帐户,当它遵循提到的模式时,只需将该帐户存储到
string
并保留其他帐户!
编辑代码
我刚刚将遵循代码中提到的模式的所有用户帐户存储在列表中。
谢谢!
根据 API 26 行为更改:
在 Android 8.0(API 级别 26)中,应用程序无法再访问用户帐户,除非身份验证者拥有帐户或用户授予该访问权限。
权限不再足够。要获得帐户访问权限,应用程序应使用 [GET_ACCOUNTS
][2] 或特定于身份验证器的方法。获得帐户访问权限后,应用程序可以调用 [AccountManager.newChooseAccountIntent()
][3] 来访问它们。AccountManager.getAccounts()