IOS——手机信号改变事件监听(兼容IOS8)

为了保证监听的手机信号正确,首先可以参考如下文章将手机的信号设置为数字表示:

http://jingyan.baidu.com/article/d169e186a3c027436611d836.html


开始实现监听事件:

(1) 定义文件CTIndicators.h

#ifndef FSTester_CTIndicators_h
#define FSTester_CTIndicators_h


#include <CoreFoundation/CoreFoundation.h>

#if __cplusplus
extern "C" {
#endif
    
#pragma mark - API
    
    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

#endif

(2) 在实现文件中引用CTIndicators.h文件并实现如下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

	//该Label用于显示信号值
    UILabel *singalLabel=[[UILabel alloc]init];
    singalLabel.backgroundColor=[UIColor clearColor];
    singalLabel.textColor=[UIColor whiteColor];
    [self.view addSubview:singalLabel];
    
	//开启一个新的线程实现监听方法,否则主线程会卡死
    NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(SignalStrenChanged:)object:nil];
    [myThread start];
}


//该方法启动信号监听
-(void)SignalStrenChanged:(id)sender{
    // Register as a listener to the kCTIndicatorsSignalStrengthNotification notification to be notified when the signal strength changed.
    CTTelephonyCenterAddObserver(CTTelephonyCenterGetDefault(), NULL, SignalStrengthDidChange, kCTIndicatorsSignalStrengthNotification, NULL, CFNotificationSuspensionBehaviorCoalesce);
	//该方法初始化信号值
    SignalStrengthInit();
    CFRunLoopRun();
}

//当信号值改变时,自动调用该方法
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 Changed: %li",raw);
    singalLabel.text=[NSString stringWithFormat:@"%li",raw];
}

static void SignalStrengthInit(){
    long int raw = 0;
    long int graded = 0;
    long int bars = 0;
    
    CTIndicatorsGetSignalStrength(&raw, &graded, &bars);
    printf("Signal Init: %li",raw);
    singalLabel.text=[NSString stringWithFormat:@"%li",raw];
}



经过测试,发现以上方法在 IOS7以上 无法获取正确的手机信号值!!!

无奈之下,请参考博客关于获取手机运营商名称的方法.......

直接获取标题栏的手机信号值!

过程如下:

statusBar ->  foregroundView -> UIStatusBarSignalStrengthItemView ->  signalStrengthRaw ->信号值


这个是UIStatusBarSignalStrengthView内所有的元素

_signalStrengthRaw

_signalStrengthBars

_enableRSSI

_showRSSI


下面直接贴代码,包括监听手机信号改变在内:(关于开启线程监听代码改变的部分我就不贴了,参考上面的)

-(void)SignalStrenChanged:(id)sender{
    CTTelephonyCenterAddObserver(CTTelephonyCenterGetDefault(), NULL, SignalStrengthDidChange, kCTIndicatorsSignalStrengthNotification, NULL, CFNotificationSuspensionBehaviorCoalesce);
    getSignalStrength();
    CFRunLoopRun();
}

static void SignalStrengthDidChange(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
        getSignalStrength();
}
void getSignalStrength(){
    NSLog(@"Get Signal Strength");
    singalLabel.text=@"";
    id type=nil;
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarSignalStrengthItemView")]) {
            type = [child valueForKeyPath:@"signalStrengthRaw"];
            break;
        }
    }
    NSInteger signalStregth=[type integerValue];
    if (signalStregth<0){
        singalLabel.text=[NSString stringWithFormat: @"%ld", (long)signalStregth];
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值