AndroidManifest.xml
<manifest xmlns:andro>
</activity>
</application>
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="5" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>
res/drawable-xxx
drawable-hdpi/icon.png 72x72
drawable-mdpi/icon.png 48x48
drawable-ldpi/icon.png 32x32
contact_manager.xml
<LinearLayout xmlns:andro/>
</LinearLayout>
在ContactManager.onCreate方法中
复选框没有选中
CheckBox mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
boolean mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
复选框事件
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
"addAccount"按钮及其事件
Button mAddAccountButton = (Button) findViewById(R.id.addContactButton);
mAddAccountButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mAddAccountButton clicked");
launchContactAdder();
}
});
在populateContactList()方法中
获取Cursor并构造SimpleCursorAdapter对象
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
获取ListView并设置Adapter对象
ListView mContactList = (ListView) findViewById(R.id.contactList);
mContactList.setAdapter(adapter);
在getContacts()方法中
获取Uri、projection、selection, selectionArgs, sortOrder
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
调用managedQuery方法
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
在launchContactAdder()方法中
跳转到ContactAdder界面
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
layout/contact_adder.xml
<ScrollView xmlns:andro/>
</TableRow>
</TableLayout>
</ScrollView>
在AccountData构造方法(如下)中
AccountData(String name, AuthenticatorDescription description)
mType = description.type;
String packageName = description.packageName;
PackageManager pm = getPackageManager();
mTypeLabel = pm.getText(packageName, description.labelId, null);
10-31 15:20:21.576: INFO/contactMgr(14321): description.type = com.sonyericsson.localcontacts
10-31 15:20:21.576: INFO/contactMgr(14321): description.packageName = com.sonyericsson.localcontacts
10-31 15:20:21.576: INFO/contactMgr(14321): description.labelId = 2130968576
10-31 15:20:21.576: INFO/contactMgr(14321): mTypeLabel = 手机联系人
10-31 15:20:21.576: INFO/contactMgr(14321): description.type = com.google
10-31 15:20:21.576: INFO/contactMgr(14321): description.packageName = com.google.android.gsf
10-31 15:20:21.576: INFO/contactMgr(14321): description.labelId = 2131165435
10-31 15:20:21.576: INFO/contactMgr(14321): mTypeLabel = Google
layout/account_entry.xml
<RelativeLayout xmlns:andro/>
</RelativeLayout>
在AccountAdapter.getDropDownView(int position, View convertView, ViewGroup parent)方法中
1. 构造convertView
if (convertView == null) {
LayoutInflater layoutInflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.account_entry, parent, false);
}
2. 获取convertView中的子View
TextView firstAccountLine = (TextView) convertView.findViewById(R.id.firstAccountLine);
TextView secondAccountLine = (TextView) convertView.findViewById(R.id.secondAccountLine);
ImageView accountIcon = (ImageView) convertView.findViewById(R.id.accountIcon);
3. 给子View赋值
AccountData data = getItem(position);
firstAccountLine.setText(data.getName());
secondAccountLine.setText(data.getTypeLabel());
Drawable icon = data.getIcon();
if (icon == null) {
icon = getResources().getDrawable(android.R.drawable.ic_menu_search);
}
accountIcon.setImageDrawable(icon);
在AccountAdapter构造方法中
1. 设置DropDownViewResource
setDropDownViewResource(R.layout.account_entry);
在接口OnAccountsUpdateListener中
1. 声明抽象方法onAccountsUpdated
public abstract void onAccountsUpdated(Account[] paramArrayOfAccount);