通常使用Android手机的使用者都会把自己的Google账号给设定在手机上,这样一来才能使用Google提供的多种服务。像是Marke、Gmail或是Google
Talk等等。也有一些软体开发者会用Google账号来当做注册的资讯使用,这样使用者就算换了其它支Android手机的话,只要Google账号不变就能继续享有原先的权利。Android
SDK 2.0中开始提供AccountManager来让开发者能存取Account信息:
import
android.accounts.Account;
import
android.accounts.AccountManager;
import
android.content.Context;
AccountManager
accountManager
= AccountManager.get(context);
Account[]
accounts
= accountManager.getAccounts();
for(Account
account:accounts){
Log.w(TAG,
"hongyan:account.name="+account.name);
Log.w(TAG,
"hongyan:account.type="+account.type);}
Log.w(TAG,
"hongyan:accounts.length="+accounts.length);
|
如果添加账户只有一个,比如:zhangyina0317@gmail.com
打印出来结果:
account.name=zhangyina0317@gmail.com
accoutn.type=com.android.exchange
account.length=1
添加权限:
android:name = "android.permission.GET_ACCOUNTS" />
|