没有在android项目中获得主电子邮件ID

时间:2017-07-15 09:15:33

标签: android

进入ManiFest的许可

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

我的xml

 <TextView
    android:id="@+id/tv_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

MainActivity.java

public class MainActivity extends AppCompatActivity {
private FirebaseAnalytics firebaseAnalytics;
ImageView GoToNewActivity;
ImageView GoToNewActivity1;
TextView tvID;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvID = (TextView) findViewById(R.id.tv_id);

    String email = getEmiailID(this);
    if(email != null) {
        tvID.setText(email);
    } else {
        tvID.setText("Account haven't been found");
    }
 private String getEmiailID(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);
    if (account == null) {
        return null;
    } else {
        return account.name;
    }
}

private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
        account = accounts[0];
    } else {
        account = null;
    }
    return account;
    }

}

未找到打印帐户,表示未收到主电子邮件。我的Android设备中有我的主电子邮件。我的代码错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个 添加此权限我的朋友

 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
 <uses-permission android:name="android.permission.READ_CONTACTS" />

创建方法的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    TextView tvID = (TextView) findViewById(R.id.tv);
    String email = getEmiailID(this);

    if (email != null) {
        tvID.setText(email);
    } else {
        tvID.setText("Account haven't been found");
    }
}

创建此方法

 private String getEmiailID(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);
    if (account == null) {
        return null;
    } else {
        return account.name;
    }
}

private Account getAccount(AccountManager accountManager) {
    if (ActivityCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return null;
    }
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
        account = accounts[0];
    } else {
        account = null;
    }
    return account;
}

答案 1 :(得分:0)

<强> AccountManager

  

此类提供对用户的集中注册表的访问   在线帐户。

权限

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

确保启用 Android's Runtime Permission

  

从Android 6.0(API级别23)开始,如果应用程序共享   管理帐户的验证者的签名,但它没有   需要&#34; GET_ACCOUNTS&#34;允许阅读有关该帐户的信息。   在Android 5.1及更低版本中,所有应用都需要&#34; GET_ACCOUNTS&#34;许可   阅读有关任何帐户的信息。

 try 
   { 
            AccountManager accountManager = AccountManager.get(MainActivity.this); 
            if (accountManager == null) 
            {
             return ""; 
            }
            else
            {
                Account[] accounts = accountManager.getAccounts(); 
                Pattern emailPatternOBJ = Patterns.EMAIL_ADDRESS; // API level 8+
                for (Account account : accounts) 
                { 

                    if (emailPatternOBJ.matcher(account.name).matches()) 
                    { 
                        System.out.println("Primary_Mail_ID"+account.name); // Return Primary Email ID .
                        return account.name; 
                    } 
                } 
                return ""; 

           } 
           catch (SecurityException e) 
           { 
            // If app doesn't have GET_ACCOUNTS permission 
            return ""; 

           }

    }