iphone 处理中断/通知

When it comes to the iPhone, phone function comes first. Incoming calls, SMS, and USSD (unstructured supplementary service data, including account balance notifications for prepaid plans) messages will supersede your application. You can program around this by watching for core telephony events and shutting down key processes as needed. The iPhone's core telephony system sends out distributed notifications to warn about these status changes.

To add core telephony notification to your program, include the following code and compile linking to the Core Telephony framework. This code adds your program as a registered observer.

id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);

To monitor the incoming notifications, you need to build the callback routine you supplied as an argument to the add observer call:

static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { ... }

The notification is sent as the name in this callback and supporting data is sent as a dictionary of information (userInfo). Use the toll-free bridging between CFStringRef and NSString to apply a simple isEqualToString: method and check which notification was sent.

The keys you will want to watch for are as follows:

kCTCallStatusChangeNotification A call was received or ended.I'm not sure when it is thrown, but I think that at the very moment of the incoming call (sometimes it is not possible to extract info about caller at this moment)

kCTCallIdentificationChangeNotification -- is thrown when call is identified. Here, you can get caller ID and\or disconnect the call:

Objective C Code:

Objective C Code:
static void callback_call(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSDictionary
*info = (NSDictionary *)userInfo;
CTCall
*call = (CTCall *)[info objectForKey:@"kCTCall"];
NSString
*caller = CTCallCopyAddress(NULL, call);
CTCallDisconnect(call);
/* or one of the following functions: CTCallAnswer
CTCallAnswerEndingActive
CTCallAnswerEndingAllOthers
CTCallAnswerEndingHeld
*/
}

kCTSMSMessageReceivedNotification An SMS message was received.

kCTUSSDSessionBeginNotification, kCTUSSDSessionStringNotification, and kCTUSSDSessionEndNotification USSD message sessions may use all three of these notifications.

During the phone call a bunch of notifications are thrown:

posted @ 2010-08-16 16:30  jrvin  阅读(932)  评论(0编辑  收藏  举报