noticationmgr对于未接来电的处理

上传人:第*** 文档编号:31312673 上传时间:2018-02-06 格式:DOCX 页数:13 大小:992.69KB
返回 下载 相关 举报
noticationmgr对于未接来电的处理_第1页
第1页 / 共13页
noticationmgr对于未接来电的处理_第2页
第2页 / 共13页
noticationmgr对于未接来电的处理_第3页
第3页 / 共13页
noticationmgr对于未接来电的处理_第4页
第4页 / 共13页
noticationmgr对于未接来电的处理_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《noticationmgr对于未接来电的处理》由会员分享,可在线阅读,更多相关《noticationmgr对于未接来电的处理(13页珍藏版)》请在金锄头文库上搜索。

1、NoticationMgr 对于未接来电的处理通过本文档将要学习 Android KK 和 Android L 两大版本对于未接来电逻辑处理上的差异,这个差异主要体现在架构设计。主要是通过以下三个方面: 取消未接来电的通知; 有未接来电时的通知; 开机显示未接来电的能知;Android KK 版本对未接来电(Missed call)的处理取消未接来电的通知当有未接来电时显示未接来电的通知当有未接来电时显示未接来电的通知是由 CallNotifier 的 onDisconnect 方法调用 NotificationMgr 的 notifyMissedCall 方法,第 8 行调用 PhoneGl

2、obals.createCallLogIntent 方法获得拉起通话记录的 intent,第 16 行的 mNumberMissedCalls+未接来电计数器自增1,第 20 至第 27 行是设置显示的信息只有一个未接来电时,会显示“未接来电/XX” ,XX 是联系人的姓名或号码,有多个未接来电时,会显示 “未接来电/n 个未接来电” ,当只有一个未接来电时,且号码有效时,增加两个 action,即“回拨” 、 “发信息” ,当参数 phoneIcon 或 photo 不为 null 时,还可以设置在托盘中显示的大图标。最后调用 NotificationManager 的 notify 方法在

3、托盘中显示未接来电通知开机后显示未接来电通知当 Phone 应用启动的时候在 PhoneGlobals 的 onCreate 方法中调用 NotificationMgr 的 init 方法进行初始化,接着调用updateNotificationAtStartup 方法在 NotificationMgr 的 updateNotificationAtStartup 方法中实例化 QueryHandler,主要用于查询通话记录、联系人等信息,第 10 至12 行是设置查询的条件,第 16 行调用 QueryHandler 的 startQuery 方法执行查询,注意在调用 startQuery 方法

4、的第一个参数CALL_LOG_TOKEN,此查询是异步执行的。整体介绍一个 QueryHandler 的结构,QueryHandler 是异常查询处理器,主要完成查询通话记录,根据通话记录查询联系人,加载联系人的头像NotificationInfo 用于存储查询出来的未接来电的基本信息通话记录完成后回调 onQueryComplete 方法,在调用 startQuery 时传入的第一个参数是 CALL_LOG_TOKEN,所以此时 case CALL_LOG_TOKEN 满足,调用 getNotificationInfo 方法,将 cursor 中的信息构造一个 NotificationInf

5、o 的实例,再次调用QueryHandler 的 startQuery 方法查询通话记录是否存在对应的联系人信息,传入的第一个参数是 CONTACT_TOKEN,调用 getNotificationInfo 方法,将 cursor 中的信息构造一个 NotificationInfo 的实例联系人信息查询完成后回调 onQueryComplete 方法,token 是 CONTACT_TOKEN,对于是一个默生的号码(通讯录人不存在对应的联系人信息)时直接调用 notifyMissedCall 方法在托盘显示未接来电通知,如果存在对应的联系人信息通过调 ContactAsyncHelper 的s

6、tartObtainPhotoAsync 方法获取对应联系人的头像, QueryHandler 已实现了 ContactAsyncHelper 的OnImageLoadCompleteListener 接口,所以联系人头像加载完成后回调 QueryHandler 的 onImageLoadComplete 方法,当联系人的头像加载完成后,回调 onImageLoadComplete 方法,最终调用 notifyMissedCall 方法在托盘中显示未接来电通知。Android L 未接来电的处理未接来电的处理逻辑由 Android KK 版本的 TeleService 转移到 Android

7、L 版本的 Telecom 中处理,但是在 TeleService中仍然保留了 KK 版本的 NotificationMgr 类的代码,但如 Android KK 版本对外的接口已经不存在了。取消未接来电的通知第三方的应用可以通过调用 TelecomManager 类的 cancelMissedCallsNotification 方法取消未接来电的消息通知,看到cancelMissedCallsNotification 的方法的命名应该感到很 nice 吧,这个就是将 Android KK 中 TelephonyManager 中移植过来的。public class TelecomManage

8、r .public void cancelMissedCallsNotification() ITelecomService service = getTelecomService();if (service != null) try service.cancelMissedCallsNotification(); catch (RemoteException e) 代码 x-xTelecomManager 调用 TelecomServiceImpl 的 cancelMissedCallsNotification 方法后,向内部的 MainThreadHandler 发送MSG_CANCEL_

9、MISSED_CALLS_NOTIFICATION 消息,处理此消息时调用了 MissedCallNotifier 的 clearMissedCalls 方法。public class TelecomServiceImpl extends ITelecomService.public void cancelMissedCallsNotification() enforceModifyPermissionOrDefaultDialer();sendRequestAsync(MSG_CANCEL_MISSED_CALLS_NOTIFICATION, 0); private final class

10、MainThreadHandler extends Handler Overridepublic void handleMessage(Message msg) if (msg.obj instanceof MainThreadRequest) MainThreadRequest request = (MainThreadRequest) msg.obj;Object result = null;switch (msg.what) .case MSG_CANCEL_MISSED_CALLS_NOTIFICATION:mMissedCallNotifier.clearMissedCalls();

11、break;.代码 x-xMissedCallNotifier 的 clearMissedCalls 做了两件事件: 将通话记录数据库中的未接来电记录的未读状态变更为已读状态; 清除在通知栏中的通知;通话记录未接来电的未读状态-已读状态,通过一个多线程异步任务的方法更新通话记录的 provider。public class MissedCallNotifier ./* Clears missed call notification and marks the call logs missed calls as read. */void clearMissedCalls() AsyncTask.

12、execute(new Runnable() Overridepublic void run() / Clear the list of new missed calls from the call log.try ContentValues values = new ContentValues();values.put(Calls.NEW, 0);values.put(Calls.IS_READ, 1);StringBuilder where = new StringBuilder();where.append(Calls.NEW);where.append( = 1 AND );where

13、.append(Calls.TYPE);where.append( = ?);mContext.getContentResolver().update(Calls.CONTENT_URI, values, where.toString(),new String Integer.toString(Calls.MISSED_TYPE) ); catch (Exception e) e.printStackTrace();Log.e(MissedCallNotifier, e, ););cancelMissedCallNotification();.代码 x-x接着调用 cancelMissedCa

14、llNotification 方法,内部通过调用 NotificationManager 的 cancel 方法清除通知栏中的未接来电notification,并将未接来电计数器 mMissedCallCount 重置为 0。public class MissedCallNotifier ./* Cancels the missed call notification. */private void cancelMissedCallNotification() / Reset the number of missed calls to 0.mMissedCallCount = 0;mNotif

15、icationManager.cancel(MISSED_CALL_NOTIFICATION_ID);.代码 x-x来电未接时在通知栏中显示 Missed call 通知在 CallsManager 的构造方法中注册了回调。public CallsManager(Context context, MissedCallNotifier missedCallNotifier,PhoneAccountRegistrar phoneAccountRegistrar) .mMissedCallNotifier = missedCallNotifier;mListeners.add(missedCallN

16、otifier);代码 x-xMissedCallNotifier 继承了 CallsManagerListenerBase,重构了 onCallStateChanged 方法,当 call 的状态发生变化时被回调。如果新的状态是 DISCONNECTED,表示有通话断开了,如果断开的原因是 MISSED,表示有一个未接来电,然后调用showMissedCallNotification 方法。public class MissedCallNotifier extends CallsManagerListenerBase .public void onCallStateChanged(Call call, int oldState, int newState) if (oldState = CallS

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号