iPhone近身感應器監測 .

來源:互聯網
上載者:User

 

The proximity sensor on the iPhone detects when the device is close to your face (or otherwise covered). There aren’t many times when using the sensor is of value, however, the Google Voice Search application has put this to good use as a means to trigger
voice recording for a search request. If you have an interest in doing something similar, read on.

Proximity Sensor Monitoring

It all begins by enabling proximity monitoring, this is followed by setting up a notification request to call a method when the proximity state changes:

view plaincopy to clipboardprint?
  1. // Enabled monitoring of the sensor
      
  2. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];  
  3.    
  4. // Set up an observer for proximity changes
      
  5. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:)   
  6.     name:@"UIDeviceProximityStateDidChangeNotification" object:nil];  

// Enabled monitoring of the sensor[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; // Set up an observer for proximity changes[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; 

The method below will be called when the sensor state is updated, a message is printed to the debug console based on the sensor proximity.

view plaincopy to clipboardprint?
  1. - (void)sensorStateChange:(NSNotificationCenter *)notification  
  2. {  
  3.   if ([[UIDevice currentDevice] proximityState] == YES)  
  4.     NSLog(@"Device is close to user.");  
  5.   else   
  6.     NSLog(@"Device is ~not~ closer to user.");  
  7. }  

- (void)sensorStateChange:(NSNotificationCenter *)notification{ if ([[UIDevice currentDevice] proximityState] == YES) NSLog(@"Device is close to user."); else NSLog(@"Device is ~not~ closer to user.");} 

Detecting Proximity Sensor

Not all iOS device have proximity sensors. The Apple API documentation states that you should enable proximity monitoring and check the proximityState, if the return value is NO, then the device does not have a sensor.

I was unable to successfully use this approach to determine if a device has a sensor. Any additional ideas or suggestions are welcome.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.