工程目录:src\ch06\ch06_QuickContactBadge 广州网站建设
我们在使用Android内置的联系人功能时会发现,在单击某个联系人后,会弹出一个可以选择的图像菜单,其中包括拨打电话、发短信等功能,如图6.12所示。
![]() |
| 图6.12 联系人菜单 |
广州网站建设
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <QuickContactBadge android:id="@+id/badge1"
- 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/quickContact
- BadgeStyleWindowSmall" />
- <QuickContactBadge android:id="@+id/badge2"
- 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/quickContact
- BadgeStyleWindowLarge" />
- </LinearLayout>
其中style属性指定了显示图像菜单的风格。如果quickContactBadgeStyleWindowLarge风格可以显示联系人的姓名,quickContactBadgeStyleWindowSmall风格只显示图像菜单。在编写代码之前,先在系统联系人中添加两个联系人,并输入不同的姓名、E-mail和电话,然后编写如下的代码:广州网站设计
- package mobile.android.ch06.quickcontactbadge;
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.ContactsContract.Contacts;
- import android.widget.QuickContactBadge;
- public class Main extends Activity
- {
- static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]
- { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.STARRED,
- Contacts.TIMES_CONTACTED, Contacts.CONTACT_PRESENCE,
- Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, Contacts.HAS_PHONE_NUMBER, };
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
- + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
- + Contacts.DISPLAY_NAME + " != '' ))";
- // 查询所有的联系人
- Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
- CONTACTS_SUMMARY_PROJECTION, select, null,
- Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
- // 将记录指针移动到第一条记录
- cursor.moveToFirst();
- // 创建第一个QuickContactBadge对象
- QuickContactBadge badge1 = (QuickContactBadge) findViewById(R.id.badge1);
- // 创建第二个QuickContactBadge对象
- QuickContactBadge badge2 = (QuickContactBadge) findViewById(R.id.badge2);
- // 获得联系人的ID
- long contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
- // 获得联系人的lookup_key
- String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
- // 将当前联系人与QuickContactBadge控件关联
- badge1.assignContactUri(Contacts.getLookupUri(contactId,lookupKey));
- // 将记录指针移动到第二条记录
- cursor.moveToNext();
- contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
- lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
- badge2.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
- }
- }
在上面的代码中涉及了Content Provider和数据库技术,这两种技术将在后面的章节详细讨论。本例先从系统联系人数据库中查询到刚才加入的两个联系人,然后分别将两个联系人与两个QuickContactBadge控件相关联。由于本例需要获得联系人信息,因此,需要在AndroidManifest.xml文件中添加如下授权代码。广州网站设计
- <uses-permission android:name="android.permission.READ_CONTACTS" />
现在来运行程序,并分别单击两个QuickContactBadge控件,会显示不带联系人姓名和带联系人姓名的两种风格的图像菜单,如图6.13和图6.14所示。
注意 QuickContactBadge控件并不会自动显示联系人的头像,要显示联系人的头像,需要像ImageView控件一样使用android:src属性设置,或使用相应的方法来装载图像资源。广州网站设计
![]() |
| 图6.13 不显示联系人姓名 |
![]() |
| 图6.14 联系人姓名 |
扩展学习:与联系人关联的其他方式
除了使用QuickContactBadge.assignContactUri方法将QuickContactBadge与联系人关联外,还可以使用如下两个方法与联系人关联。
- QuickContactBadge.assignContactFromEmail
- QuickContactBadge.assignContactFromPhone
这两个方法的定义如下:
- public void assignContactFromEmail(String emailAddress, boolean lazyLookup)
- public void assignContactFromPhone(String phoneNumber, boolean lazyLookup)
这两个方法分别可以通过E-mail和电话号码与联系人关联。如果lazyLookup参数值为true,并不会立即通过E-mail或Phone查找联系人,直接QuickContactBadge控件被单击。如果使用这两个方法与联系人关联与assignContactUri方法有如下两点不同。
如果有多个联系人使用了同一个电话或E-mail,则只显示第一个查到的联系人信息。
使用这两个方法与联系人关联,单击QuickContactBadge控件并不会显示图像菜单,而会直接跳到系统联系人界面。






