android Account

android


public class MyContacts {
	private Context mContext;
	private static final String TAG = "Contacts";

	public MyContacts(Context context) {
		this.mContext = context;
	}

	/**
	 * 重通讯录中获取用户配置信息
	 */
	public void getUserProfiles() {
		// API 14
		String[] mProjection = new String[] { Profile._ID,
				Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY,
				Profile.PHOTO_THUMBNAIL_URI };

		Cursor mProfileCursor = mContext.getContentResolver().query(
				Profile.CONTENT_URI, mProjection, null, null, null);
		while (mProfileCursor.moveToNext()) {
			if (mProfileCursor.getInt(mProfileCursor
					.getColumnIndex(Profile.IS_USER_PROFILE)) == 1) {
				Log.i(TAG, "Profile._ID" + mProfileCursor.getString(0));
				Log.i(TAG,
						"\nDISPLAY_NAME_PRIMARY" + mProfileCursor.getString(1));
				Log.i(TAG, "\nLOOKUP_KEY" + mProfileCursor.getString(2));
				Log.i(TAG,
						"\nPHOTO_THUMBNAIL_URI" + mProfileCursor.getString(3));
			}
		}
	}

	/**
	 * 获取设备上所有的账户信息
	 */
	public void getUserAccount() {

		AccountManager am = AccountManager.get(mContext);
		Account[] accounts = am.getAccounts();
		HashSet<String> contactAccountTypes = new HashSet<String>();
		SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();

		Log.i(TAG, "所有同步适配器信息:\n");
		for (SyncAdapterType sync : syncs) {
			Log.i(TAG, "type:" + sync.accountType + " autohrity:"
					+ sync.authority);
			if (ContactsContract.AUTHORITY.equals(sync.authority)) {
				// 获取联系人账户
				contactAccountTypes.add(sync.accountType);
			}
		}
		
		// 符合联系人账户类型的账户 添加到list
		ArrayList<Account> contactAccounts = new ArrayList<Account>();
		for (Account acct : accounts) {
			if (contactAccountTypes.contains(acct.type)) {
				contactAccounts.add(acct);
			}
		}
		
		Log.i(TAG, "设备全部账户信息\n");
		for (Account ac : accounts) {
			Log.i(TAG, "name: "+ ac.name + "  type:" + ac.type);
		}
		
		//获取账户的认证器信息
		Log.i(TAG, "设备账户所有的认证器信息:\n\n");
		AuthenticatorDescription[] accountTypes = AccountManager.get(mContext).getAuthenticatorTypes();
		for (AuthenticatorDescription des : accountTypes) {
			Log.i(TAG, "type:" + des.type + "  packageName:"+ des.packageName );
		}
		
		
		
		Log.i(TAG, "联系人账户信息:\n");
		for (Account ac : contactAccounts) {
			Log.i(TAG, "name:" + ac.name + "  type:" + ac.type);
		}
		
		
	}

}

 

 

------------------------------------------------------------------ 实现账户变化监听器 -------------------------------------------------------------------------------------------

OnAccountsUpdateListener() 获取到所有的account。随后onDestory()取消该监听器

implements OnAccountsUpdateListener<pre class="java" name="code"> 
//添加监听器
AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);

 
 
package android.accounts;

/**
 * An interface that contains the callback used by the AccountMonitor
 */
public interface OnAccountsUpdateListener {
    /**
     * This invoked when the AccountMonitor starts up and whenever the account
     * set changes.
     * @param accounts the current accounts
     */
    void onAccountsUpdated(Account[] accounts);
}
<pre class="java" name="code">    /**
     * Called when this activity is about to be destroyed by the system.
     */
    @Override
    public void onDestroy() {
        // Remove AccountManager callback
        AccountManager.get(this).removeOnAccountsUpdatedListener(this);
        super.onDestroy();
    }


 

 

 

    public void onAccountsUpdated(Account[] a) {
        Log.i(TAG, "Account list update detected");
        // Clear out any old data to prevent duplicates
        mAccounts.clear();

        // Get account data from system 账户认证器数组
        AuthenticatorDescription[] accountTypes = AccountManager.get(this).getAuthenticatorTypes();

        // Populate tables
        for (int i = 0; i < a.length; i++) {
            // The user may have multiple accounts with the same name, so we need to construct a
            // meaningful display name for each.将账户对应的AuthenticatorDescription绑定 显示图片和账户Label	
            String systemAccountType = a[i].type;
            AuthenticatorDescription ad = getAuthenticatorDescription(systemAccountType,
                    accountTypes);
            AccountData data = new AccountData(a[i].name, ad);
            mAccounts.add(data);
        }

        // Update the account spinner
        mAccountAdapter.notifyDataSetChanged();
    }


 

    /**
     * Obtain the AuthenticatorDescription for a given account type.
     * @param type The account type to locate.
     * @param dictionary An array of AuthenticatorDescriptions, as returned by AccountManager.
     * @return The description for the specified account type.
     */
    private static AuthenticatorDescription getAuthenticatorDescription(String type,
            AuthenticatorDescription[] dictionary) {
        for (int i = 0; i < dictionary.length; i++) {
            if (dictionary[i].type.equals(type)) {
                return dictionary[i];
            }
        }
        // No match found
        throw new RuntimeException("Unable to find matching authenticator");
    }



 

   /**
     * A container class used to repreresent all known information about an account.
     */
    private class AccountData {
        private String mName;
        private String mType;
        private CharSequence mTypeLabel;
        private Drawable mIcon;

        /**
         * @param name The name of the account. This is usually the user's email address or
         *        username.
         * @param description The description for this account. This will be dictated by the
         *        type of account returned, and can be obtained from the system AccountManager.
         */
        public AccountData(String name, AuthenticatorDescription description) {
            mName = name;
            if (description != null) {
                mType = description.type;

                // The type string is stored in a resource, so we need to convert it into something
                // human readable.
                String packageName = description.packageName;
                PackageManager pm = getPackageManager();

                if (description.labelId != 0) {
                    mTypeLabel = pm.getText(packageName, description.labelId, null);
                    if (mTypeLabel == null) {
                        throw new IllegalArgumentException("LabelID provided, but label not found");
                    }
                } else {
                    mTypeLabel = "";
                }

                if (description.iconId != 0) {
                    mIcon = pm.getDrawable(packageName, description.iconId, null);
                    if (mIcon == null) {
                        throw new IllegalArgumentException("IconID provided, but drawable not " +
                                "found");
                    }
                } else {
                    mIcon = getResources().getDrawable(android.R.drawable.sym_def_app_icon);
                }
            }
        }

        public String getName() {
            return mName;
        }

        public String getType() {
            return mType;
        }

        public CharSequence getTypeLabel() {
            return mTypeLabel;
        }

        public Drawable getIcon() {
            return mIcon;
        }

        public String toString() {
            return mName;
        }
    }


 





 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值