获取android中的帐户列表

时间:2017-03-21 04:55:53

标签: android listview

我正在开发一个应用程序,我需要在用户的手机中查看帐户列表。我已经去了这个代码,它似乎工作得很好。我面临的唯一问题是我想获得一个帐户清单,而不仅仅是一个,那么这将如何实现? 我很确定需要列表视图,但我没有得到任何关于如何在这里使用它的线索。

代码:

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" />

1 个答案:

答案 0 :(得分:5)

按以下方式更改您的实施:

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并留下其他帐户!

已编辑的代码

我刚刚存储了所有符合代码中提到的模式的用户帐户。

谢谢!