QuickContacBadge译为联系人快捷标识,用于显示一张图片,点击该图后弹出相关的快捷功能,如打电话、发短信等。
QuickContacBadge提供了三个方法指定联系人的信息:
1、assignContactUri(Uri contactUri) : 联系人Uri
2、assignContactFromPhone(String phonNumber,boolean lazyLookup) : 从电话号码加载联系人信息。phonNumber: 电话号码 ;lazyLookup:是否延迟加载。注:这个方法应该只在不知道联系人uri的情况下使用(该方法会多做一次查询)。
3、assignContactFromEmail(String emailAddress, boolean lazyLookup):从邮箱号码加载联系人信息。 emailAddress:邮箱地址; lazyLookup:是否延迟加载。
assignContactFromPhone(String phonNumber,boolean lazyLookup) 的源码如下:
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) {
mContactPhone = phoneNumber;
if (!lazyLookup) {
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
PHONE_LOOKUP_PROJECTION, null, null, null);
} else {
mContactUri = null;
onContactUriChanged();
}
}
可以看到如果是lazyLookup为false,则立即加载信息。
通过源码:
@Override
public void onClick(View v) {
if (mContactUri != null) {
QuickContact.showQuickContact(getContext(), QuickContactBadge.this, mContactUri,
QuickContact.MODE_LARGE, mExcludeMimes);
} else if (mContactEmail != null) {
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, mContactEmail,
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
EMAIL_LOOKUP_PROJECTION, null, null, null);
} else if (mContactPhone != null) {
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, mContactPhone,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
PHONE_LOOKUP_PROJECTION, null, null, null);
} else {
// If a contact hasn't been assigned, don't react to click.
return;
}
}
可以看到,当用户点击后是通过一个handler去加载数据的,QueryHandler的代码如下:
private class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Uri lookupUri = null;
Uri createUri = null;
boolean trigger = false;
try {
switch(token) {
case TOKEN_PHONE_LOOKUP_AND_TRIGGER:
trigger = true;
createUri = Uri.fromParts("tel", (String)cookie, null);
//$FALL-THROUGH$
case TOKEN_PHONE_LOOKUP: {
if (cursor != null && cursor.moveToFirst()) {
long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX);
String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX);
lookupUri = Contacts.getLookupUri(contactId, lookupKey);
}
break;
}
case TOKEN_EMAIL_LOOKUP_AND_TRIGGER:
trigger = true;
createUri = Uri.fromParts("mailto", (String)cookie, null);
//$FALL-THROUGH$
case TOKEN_EMAIL_LOOKUP: {
if (cursor != null && cursor.moveToFirst()) {
long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX);
String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX);
lookupUri = Contacts.getLookupUri(contactId, lookupKey);
}
break;
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
mContactUri = lookupUri;
onContactUriChanged();
if (trigger && lookupUri != null) {
// Found contact, so trigger QuickContact
QuickContact.showQuickContact(getContext(), QuickContactBadge.this, lookupUri,
QuickContact.MODE_LARGE, mExcludeMimes);
} else if (createUri != null) {
// Prompt user to add this person to contacts
final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri);
getContext().startActivity(intent);
}
}
}
下面是个简单的使用QuickContacBadge的示例,首先看看效果:
1、widget_quick_contact_alyout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<QuickContactBadge
android:id="@+id/show_quick_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:src="@drawable/quick_contact" />
</LinearLayout>
2、activity——WidgetQuickContactActivity.java
package com.xy.zt.selfdefinewieget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.QuickContactBadge;
public class WidgetQuickContactActivity extends Activity {
private QuickContactBadge mQcb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_quick_contact_alyout);
init();
}
private void init() {
mQcb = (QuickContactBadge) findViewById(R.id.show_quick_contact);
mQcb.assignContactFromPhone("02885322828", true);
}
}
3、在ViewData.java中添加如下代码(此部分内容可选),若有不清楚的地方参见
一步一步学android控件(之一) —— 开始篇
public static final int QUICK_CONTACT_BADGE_ID = SEEK_BAR_ID + 1;
public static final String QUICK_CONTACT_BADGE_NAME = "QuickContactBadge";
private static final ViewData mQuickContact = new ViewData(QUICK_CONTACT_BADGE_NAME,
QUICK_CONTACT_BADGE_ID);
View_Datas.add(mQuickContact);
WidgetAdapter的handleItemClicked方法中添加如下代码:
case ViewData.QUICK_CONTACT_BADGE_ID:
intent.setClass(mContext, WidgetQuickContactActivity.class);
mContext.startActivity(intent);
break;