0 Comments

QuickContactBadge与联系人菜单

发布于:2012-11-21  |   作者:广州网站建设  |   已聚集:人围观
QuickContactBadge与联系人菜单

工程目录:src\ch06\ch06_QuickContactBadge    广州网站建设

我们在使用Android内置的联系人功能时会发现,在单击某个联系人后,会弹出一个可以选择的图像菜单,其中包括拨打电话、发短信等功能,如图6.12所示。

图6.12  联系人菜单
这个功能实际上是由QuickContactBadge控件完成的。QuickContactBadge是ImageView的子类,因此,QuickContactBadge完全可以作为一个ImageView来使用。但QuickContactBadge还可以查询系统的联系人列表,并根据联系人的相关信息显示如图6.12所示的快捷菜单。现在我们先在布局文件中放两个QuickContactBadge控件。
广州网站建设
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.     <QuickContactBadge android:id="@+id/badge1" 
  6.         android:layout_marginLeft="2dip" android:layout_marginRight="14dip"   
  7.         android:layout_marginTop="4dip" android:layout_marginBottom="3dip" 
  8.         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" 
  9.         android:layout_height="wrap_content" android:layout_width="wrap_content" 
  10.         android:src="@drawable/ic_contact_picture" style="?android:attr/quickContact  
  11.         BadgeStyleWindowSmall" /> 
  12.     <QuickContactBadge android:id="@+id/badge2" 
  13.         android:layout_marginLeft="2dip" android:layout_marginRight="14dip"   
  14.         android:layout_marginTop="4dip" android:layout_marginBottom="3dip" 
  15.         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" 
  16.         android:layout_height="wrap_content" android:layout_width="wrap_content" 
  17.         android:src="@drawable/ic_contact_picture" style="?android:attr/quickContact  
  18.         BadgeStyleWindowLarge" /> 
  19. </LinearLayout> 

其中style属性指定了显示图像菜单的风格。如果quickContactBadgeStyleWindowLarge风格可以显示联系人的姓名,quickContactBadgeStyleWindowSmall风格只显示图像菜单。在编写代码之前,先在系统联系人中添加两个联系人,并输入不同的姓名、E-mail和电话,然后编写如下的代码:广州网站设计


  1. package mobile.android.ch06.quickcontactbadge;  
  2.  
  3. import android.app.Activity;  
  4. import android.database.Cursor;  
  5. import android.os.Bundle;  
  6. import android.provider.ContactsContract.Contacts;  
  7. import android.widget.QuickContactBadge;  
  8.  
  9. public class Main extends Activity  
  10. {  
  11.     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]  
  12.     { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.STARRED,  
  13.             Contacts.TIMES_CONTACTED, Contacts.CONTACT_PRESENCE,  
  14.             Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, Contacts.HAS_PHONE_NUMBER, };  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState)  
  17.     {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("  
  21.                 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("  
  22.                 + Contacts.DISPLAY_NAME + " != '' ))";  
  23.         //  查询所有的联系人  
  24.         Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,  
  25.                 CONTACTS_SUMMARY_PROJECTION, select, null,  
  26.                 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");  
  27.         //  将记录指针移动到第一条记录  
  28.         cursor.moveToFirst();  
  29.         //  创建第一个QuickContactBadge对象  
  30.         QuickContactBadge badge1 = (QuickContactBadge) findViewById(R.id.badge1);  
  31.         //  创建第二个QuickContactBadge对象  
  32.         QuickContactBadge badge2 = (QuickContactBadge) findViewById(R.id.badge2);  
  33.         //  获得联系人的ID  
  34.         long contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));  
  35.         //  获得联系人的lookup_key  
  36.         String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));   
  37.         //  将当前联系人与QuickContactBadge控件关联  
  38.         badge1.assignContactUri(Contacts.getLookupUri(contactId,lookupKey));  
  39.         //  将记录指针移动到第二条记录  
  40.         cursor.moveToNext();  
  41.         contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));  
  42.         lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));   
  43.         badge2.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));  
  44.     }  
  45. }  

在上面的代码中涉及了Content Provider和数据库技术,这两种技术将在后面的章节详细讨论。本例先从系统联系人数据库中查询到刚才加入的两个联系人,然后分别将两个联系人与两个QuickContactBadge控件相关联。由于本例需要获得联系人信息,因此,需要在AndroidManifest.xml文件中添加如下授权代码。广州网站设计


  1. <uses-permission android:name="android.permission.READ_CONTACTS" /> 

现在来运行程序,并分别单击两个QuickContactBadge控件,会显示不带联系人姓名和带联系人姓名的两种风格的图像菜单,如图6.13和图6.14所示。

注意 QuickContactBadge控件并不会自动显示联系人的头像,要显示联系人的头像,需要像ImageView控件一样使用android:src属性设置,或使用相应的方法来装载图像资源。广州网站设计

 

图6.13  不显示联系人姓名
图6.14  联系人姓名

扩展学习:与联系人关联的其他方式

除了使用QuickContactBadge.assignContactUri方法将QuickContactBadge与联系人关联外,还可以使用如下两个方法与联系人关联。


  1. QuickContactBadge.assignContactFromEmail  
  2. QuickContactBadge.assignContactFromPhone 

这两个方法的定义如下:


  1. public void assignContactFromEmail(String emailAddress, boolean lazyLookup)  
  2. public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) 

这两个方法分别可以通过E-mail和电话号码与联系人关联。如果lazyLookup参数值为true,并不会立即通过E-mail或Phone查找联系人,直接QuickContactBadge控件被单击。如果使用这两个方法与联系人关联与assignContactUri方法有如下两点不同。

如果有多个联系人使用了同一个电话或E-mail,则只显示第一个查到的联系人信息。

使用这两个方法与联系人关联,单击QuickContactBadge控件并不会显示图像菜单,而会直接跳到系统联系人界面。

飞机