详见在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;
}
}
分享到:
相关推荐
基于android studio的联系人读取及电话拨打,参考了《第一行代码》的思路
1.Android调用系统通讯录 2.选择联系人 3.拨打电话 4.添加 查询联系人
输入联系人的基本信息,并可根据用户需求增加个性化信息如头像、姓名、手机号码、办室电话、家庭电话、职务职称、单位名称、地址、邮政编码、Email、其他联系方式、备注这些信息,击确认返回主界面。点击通信录中一...
一个通过电话簿的内容提供器获取电话簿中联系人及相关信息进行显示的电话通讯录
android手机号码归属地查询源码内附SQlite数据库,并且有查询示例。.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
通过contentprovider提供的uri查询手机电话簿的联系人信息,包括姓名,电话,头像(获取的是本机保存的联系人信息,没有获取SIM卡里面联系人的信息)
Android---获取手机联系人
自己学习Android的时候写的项目: 安卓电话簿课程设计(实现对联系人的增删改查)
先在模拟器随便添加两个联系人,运行程序,再打开联系人,发现多了联系人“张三”,并且日志输出了三给安溪人的姓名和手机号码还有号码类型。(运行多次,所以插入的id不为3了) 1.activity_main.xml页面暂时用不到...
点击通讯录选项菜单中的“查找联系人”菜单项,进入搜索界面,能通过姓名、手机号码、单位名称等信息查找联系人,将所有符合查询条件的联系人通过列表控件显示。 快捷菜单功能 在联系人主界面上长按一个已存在的联系...
1、自定义view——–字母索引列表view 2、listview数据的展示 3、查询本地联系人的相关信息,如姓名、电话号 4、6.0权限适配
android实现手机通讯录,参照手机中的通讯录软件,编写一个简单的通讯录程序,包括添加联系人、删除联系人、修改联系人、查询联系人等基本功能,每条记录至少包含联系人姓名和电话号码两个字段 。
使用Android开发 简单 强悍 适合初学者,代码具有登录界面,注册界面,添加通讯录联系人,修改联系人,删除联系人,查询联系人等多个功能,简单使用十分适合初学者练习 参考使用 内有apk 可直接在手机上运行
我软件用Android写的,实现了本地联系人基本信息以及头像的读取,联系人头像的修改,联系人的增删改查功能的实现,以及联系人的批量删除.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行...
2. 具备电话号码查询功能,可以根据姓名的汉字、拼音、拼音首字母、电话号码进行查询 3. 具有来电号码显示姓名的功能 实际运行的界面比模拟器上好看的多。 后面的RAR文件为全部源代码,可以直接在Eclipse中导入...
Android系统中的联系人也是通过ContentProvider来对外提供数据的,我们这里实现获取所有联系人、通过电话号码获取联系人、添加联系人、使用事务添加联系人。 获取所有联系人 1. Android系统中的联系人也是通过...
使用contentprovide查询删除联系人,根据电话号码删除短息
运用Android Studio练习的一个通讯录App,其中存储数据用的是SQLite数据库,能够实现联系人信息的增、删、改、查。 并且还能实现打电话功能和发短信功能。
使用android通讯录中的内容提供者,实现查询和增加联系人。经测试,可用
这个通讯录源码里有注释,尤其对学习是相当有帮助的,学习一下列表的使用,本源码中的联系人按照列表表示形式:姓名 + 移动电话的方式显示,这在Android应用编写中,是比较常见的方式,另外本实例中还包括一些菜单的...