使用 CoreTelephony 和CTTelephonyCenter
观察者:
#include <CoreTelephony/CoreTelephony.h>
// Event handler
static void SignalStrengthDidChange(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
long int raw = 0;
long int graded = 0;
long int bars = 0;
CTIndicatorsGetSignalStrength(&raw, &graded, &bars);
printf("Signal strength changed! Raw: %li, graded: %li bars: %li\n", raw, graded, bars);
// Prints something like:
// Signal strength changed! Raw: -96, graded: 27 bars: 3
}
在另一个函数中注册处理程序:
// Register as a listener to the kCTIndicatorsSignalStrengthNotification notification to be notified when the signal strength changed.
CTTelephonyCenterAddObserver(CTTelephonyCenterGetDefault(), NULL, SignalStrengthDidChange, kCTIndicatorsSignalStrengthNotification, NULL, CFNotificationSuspensionBehaviorCoalesce);
// Get the initial strength.
SignalStrengthDidChange();
CFRunLoopRun();
改编自关于 CTIndicators 的 iPhone Dev Wiki 文章。
我相信这些方法不再存在于任何大于 8.4(?)的 iOS SDK 中。要访问它们,请创建一个新标头,用于外部函数和常量:
#include <CoreFoundation/CoreFoundation.h>
#if __cplusplus
extern "C" {
#endif
#pragma mark - API
/* This API is a mimic of CFNotificationCenter. */
CFNotificationCenterRef CTTelephonyCenterGetDefault();
void CTTelephonyCenterAddObserver(CFNotificationCenterRef center, const void *observer, CFNotificationCallback callBack, CFStringRef name, const void *object, CFNotificationSuspensionBehavior suspensionBehavior);
void CTTelephonyCenterRemoveObserver(CFNotificationCenterRef center, const void *observer, CFStringRef name, const void *object);
void CTTelephonyCenterRemoveEveryObserver(CFNotificationCenterRef center, const void *observer);
void CTIndicatorsGetSignalStrength(long int *raw, long int *graded, long int *bars);
#pragma mark - Definitions
/* For use with the CoreTelephony notification system. */
extern CFStringRef kCTIndicatorsSignalStrengthNotification;
#if __cplusplus
}
#endif