安卓:如何获得配置的电子邮件帐户地址编程
问题描述:
我用下面的代码来获得配置的帐户名安卓:如何获得配置的电子邮件帐户地址编程
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
Log.d("Account", "Name " + account.name);
}
但我需要的电子邮件ID的配置的Microsoft Exchange占,因为我们可以更改帐户的名称(它不需要是唯一的)。
在此先感谢
答
我认为这段代码对你有帮助。
这里是我的代码:
AccountManager accManager = AccountManager.get(context);
Account acc[] = accManager.getAccounts();
int accCount = acc.length;
AppConstants.accOnDevice = new Vector<String>();
for(int i = 0; i < accCount; i++){
//Do your task here...
}
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
答
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
String gmail = null;
Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (gmailPattern.matcher(account.name).matches()) {
gmail = account.name;
}
}
Toast.makeText(this, gmail, Toast.LENGTH_LONG).show();
}
答
此代码正常工作
public class RegisteredEmailAccounts extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registered_email_account);
final TextView accountsData = (TextView) findViewById(R.id.accounts);
String possibleEmail="";
try{
possibleEmail += "************* Get Registered Gmail Account
*************\n\n";
Account[] accounts =
AccountManager.get(this).getAccountsByType("com.google");
for (Account account : accounts) {
possibleEmail += " --> "+account.name+" : "+account.type+" , \n";
possibleEmail += " \n\n";
}
}
catch(Exception e)
{
Log.i("Exception", "Exception:"+e) ;
}
try{
possibleEmail += "**************** Get All Registered Accounts
*****************\n\n";
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
possibleEmail += " --> "+account.name+" : "+account.type+" , \n";
possibleEmail += " \n";
}
}
catch(Exception e)
{
Log.i("Exception", "Exception:"+e) ;
}
// Show on screen
accountsData.setText(possibleEmail);
Log.i("Exception", "mails:"+possibleEmail) ;
}
}
答
试试这个代码,肯定会为你
AccountManager accManager = AccountManager.get(getApplicationContext());
Account acc[] = accManager.getAccountsByType("com.google");
int accCount = acc.length;
for(int i = 0; i < accCount; i++)
{
//Do your task here...
Toast.makeText(getApplicationContext(),acc[i].name,Toast.LENGTH_SHORT).show();
}
原生这不会工作工作电子邮件应用程序,允许Microsoft交换帐户原生电子邮件应用程序允许用户更改名称 – Sudarshan
可能是此代码对您有用....... Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(“message/rfc822”); PackageManager pkgManager = context.getPackageManager(); 列表 activities = pkgManager.queryIntentActivities(intent,0); –
这对于获取电子邮件ID有什么用处? – Sudarshan