QuickContactsDemo示例介绍了
1.使用Content Provider来访问Android系统的Contacts 数据库;
2.ResourceCursorAdapter的使用
3.QuickContactBadge使用
Content Provider为不同应用之间共享数据提供了统一的接口,通过对底层数据源的抽象,Content Provider实现了应用程序代码和数据层分离。Android平台对大部分的系统数据库都提供了对应的Content Provider接口:
- Browser: 读取和修改Bookmark,Browser history或Web Searches。
- CallLog: 查看或是更新Call History(打入电话或是打出电话,未接电话等)
- Contacts: 检索,修改或存储通讯录。
- MediaStore: 访问媒体库(包括声音,视频,图像等)。
- Settings: 访问系统设置,查看或是修改蓝牙设置,铃声设置等。
Android系统的每个Content Provider都定义了一个CONTENT_URI,功能类似于数据库的名称。Android中每个Context对象(如Activity)对含有一个ContentResolver,ContentResolver可以根据CONTENT_URI获取对应的Content Provider:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
Cursor c =
getContentResolver().query(Contacts.CONTENT_URI,
CONTACTS_SUMMARY_PROJECTION,
select,
null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
startManagingCursor(c);
ContactListItemAdapter adapter
= new ContactListItemAdapter(this,
R.layout.quick_contacts, c);
setListAdapter(adapter);
}
getContentResolver() 取的ContentResolver对象,它的Query方法定义如下:
public final Cursorquery(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder)
- Uri: 需要访问的Content Provider对应的URI,如通讯录的URI为Contacts.CONTENT_URI。
- Projection: 需要返回的表的列名,如为NULL,则返回表的全部列。
- Selection: 查询数据表的条件,相当于SQL 的Where语句。
- selectionArgs: 相当于SQL查询条件的查询参数?
- sortOrder: 相当于SQL查询的Order语句,查询排序,为空时,返回记录的缺省顺序。
可以看得出,Content Provider和 数据库的用法非常类似。query返回的对象为Cusor ,有Cursor对象后就可以和访问数据库表一样来insert ,delete ,update 数据库。
startManagingCursor(c); 让Activity来管理cursor 的生命周期。
此外访问Content Provider还需要合适的权限才能正确访问,比如读写通讯录,需要在AndroidManifest.xml设置:
<uses-permission android:name=”android.permission.READ_CONTACTS” />
<uses-permission android:name=”android.permission.WRITE_CONTACTS” />
才能有权限访问通信录。
注:如果在模拟器上运行这个示例,需要在Contacts添加几个Contacts,否则这个例子没有显示。
当在头像上单击一下会显示:
在
ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);
中的quick_contacts.xml文件,其中包含一个QuickContactBadge控件,即我们看到的头像
<QuickContactBadge
android:id="@+id/badge"
android:layout_marginLeft="2dip"
android:layout_marginRight="14dip"
android:layout_marginTop="4dip"
android:layout_marginBottom="3dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:src="@drawable/ic_contact_picture"
style="?android:attr/quickContactBadgeStyleWindowSmall" />
QuickContactBadge继承自ImageView,所以所有ImageView的函数都可以用来设置QuickContactBadge。
QuickContactBadge是为了方便开发者使用QuickContact,设计的一个组件,开发者只需要在自己的Layout中加入该组件,便可以方便的使用。而且该组件还支持根据邮箱、电话号码等指定某个联系人。实际上,QuickContactBadge在单击的时候调用了ContactContract中的函数来显示QuickContact。
private final class ContactListItemAdapter extends ResourceCursorAdapter {
public ContactListItemAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
TextView nameView = cache.nameView;
QuickContactBadge photoView = cache.photoView;
// Set the name
cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
int size = cache.nameBuffer.sizeCopied;
cache.nameView.setText(cache.nameBuffer.data, 0, size);
final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = super.newView(context, cursor, parent);
ContactListItemCache cache = new ContactListItemCache();
cache.nameView = (TextView) view.findViewById(R.id.name);
cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
view.setTag(cache);
return view;
}
}
final static class ContactListItemCache {
public TextView nameView;
public QuickContactBadge photoView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
}
在上面的源码中自定义了一个ContactListItemAdapter继承于ResourceCursorAdapter。
ResourceCursorAdapter继承于CursorAdapter,它包含LayoutInflater对象,可以把xml转化成view。
ResourceCursorAdapter
继承于CursorAdapter,它包含LayoutInflater对象,可以把xml转化成view。