详见在Android 2.2源码 packages/apps/Phone/tests/src/com/android/phone/NotificationMgr.java 模块中异步查询机制:

(1)开始查询

// instantiate query handler

mQueryHandler = new QueryHandler(mContext.getContentResolver());

// setup query spec, look for all Missed calls that are new.

StringBuilder where = new StringBuilder("type=");

where.append(Calls.MISSED_TYPE);

where.append(" AND new=1");

// start the query

mQueryHandler.startQuery(CALL_LOG_TOKEN, null, Calls.CONTENT_URI, CALL_LOG_PROJECTION,

where.toString(), null, Calls.DEFAULT_SORT_ORDER);

(2)异步查询类

/**
* Class used to run asynchronous queries to re-populate
* the notifications we care about.
*/
private class QueryHandler extends AsyncQueryHandler {

/**
* Used to store relevant fields for the Missed Call
* notifications.
*/
private class NotificationInfo { //Modle
public String name;
public String number;
public String label;
public long date;
public int subscription;
}

public QueryHandler(ContentResolver cr) {
super(cr);
}

/**
* Handles the query results. There are really 2 steps to this,
* similar to what happens in RecentCallsListActivity.
* 1. Find the list of missed calls
* 2. For each call, run a query to retrieve the caller's name.
*/
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// TODO: it would be faster to use a join here, but for the purposes
// of this small record set, it should be ok.

// Note that CursorJoiner is not useable here because the number
// comparisons are not strictly equals; the comparisons happen in
// the SQL function PHONE_NUMBERS_EQUAL, which is not available for
// the CursorJoiner.

// Executing our own query is also feasible (with a join), but that
// will require some work (possibly destabilizing) in Contacts
// Provider.

// At this point, we will execute subqueries on each row just as
// RecentCallsListActivity.java does.
switch (token) {
case CALL_LOG_TOKEN:
if (DBG) log("call log query complete.");

// initial call to retrieve the call list.
if (cursor != null) {
while (cursor.moveToNext()) {
// for each call in the call log list, create
// the notification object and query contacts
NotificationInfo n = getNotificationInfo (cursor);

if (DBG) log("query contacts for number: " + n.number); //从号码中查询联系人

mQueryHandler.startQuery(CONTACT_TOKEN, n,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, n.number),
PHONES_PROJECTION, null, null, PhoneLookup.NUMBER);

}

if (DBG) log("closing call log cursor.");
cursor.close();
}
break;
case CONTACT_TOKEN:
if (DBG) log("contact query complete.");

// subqueries to get the caller name.
if ((cursor != null) && (cookie != null)){
NotificationInfo n = (NotificationInfo) cookie;

if (cursor.moveToFirst()) {
// we have contacts data, get the name.
if (DBG) log("contact :" + n.name + " found for phone: " + n.number);
n.name = cursor.getString(
cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
}

// send the notification
if (DBG) log("sending notification.");
if (TelephonyManager.isDsdsEnabled()) {
notifyMissedCall(n.name, n.number, n.label, n.date, n.subscription);
} else {
notifyMissedCall(n.name, n.number, n.label, n.date);
}

if (DBG) log("closing contact cursor.");
cursor.close();
}
break;
default:
}
}

/**

* Factory method to generate a NotificationInfo object given a

* cursor from the call log table.

*/

private final NotificationInfo getNotificationInfo(Cursor cursor) {

NotificationInfo n = new NotificationInfo();

n.name = null;

n.number = cursor.getString(cursor.getColumnIndexOrThrow(Calls.NUMBER));

n.label = cursor.getString(cursor.getColumnIndexOrThrow(Calls.TYPE));

n.date = cursor.getLong(cursor.getColumnIndexOrThrow(Calls.DATE));

// make sure we update the number depending upon saved values in

// CallLog.addCall(). If either special values for unknown or

// private number are detected, we need to hand off the message

// to the missed call notification.

if ( (n.number.equals(CallerInfo.UNKNOWN_NUMBER)) ||

(n.number.equals(CallerInfo.PRIVATE_NUMBER)) ||

(n.number.equals(CallerInfo.PAYPHONE_NUMBER)) ) {

n.number = null;

}

if (DBG) log("NotificationInfo constructed for number: " + n.number);

return n;

}

}

更多相关文章

  1. android 获取Gps信息的程序源码
  2. Android AOSP源码下载编译
  3. android 项目中接入flutter模块(部分页面)
  4. Android ADB 源码分析总结
  5. Android OkHttp3源码分析
  6. 关于Android多项目依赖在Eclipse中无法关联源代码的问题解决 (an
  7. android音乐播放器源码
  8. Android上传文件,客户端+服务器源码
  9. php直播源码安卓自定义Dialog设置自动消失

随机推荐

  1. Android自动化测试之MonkeyRunner之Monke
  2. Android:入门级Android开发者应该知道的
  3. Android onMeasure自定义
  4. android 触发器
  5. Utilize Avahi Libraries in Android for
  6. Android sdk tool android 命令参数
  7. Android -Arouter使用说明
  8. Android 获取版本号
  9. android 一些数据转换方法
  10. Unity3d - Android Build : Android SDK