Windows传感器开发之请求用户许可
2015-09-24 12:25
183 查看
本主题介绍如何向用户请求许可以使用传感器。有关传感器 API 中的许可的背景信息,请参见管理用户许可。
下面的示例演示某些您可选择以请求用户许可的常见方案。
下面的示例代码简单地使用异步方法调用按类型为从传感器管理器中检索的所有传感器请求许可。平台将打开一个对话框,提示用户仅启用尚未启用的传感器。若要在此例中确定用户是否已启用了任何传感器,您必须处理ISensorEvents::OnStateChanged 事件。
下面的示例代码将在尝试从特定传感器检索数据报告失败的情况下提示用户许可使用传感器。
下面的示例演示某些您可选择以请求用户许可的常见方案。
下面的示例代码简单地使用异步方法调用按类型为从传感器管理器中检索的所有传感器请求许可。平台将打开一个对话框,提示用户仅启用尚未启用的传感器。若要在此例中确定用户是否已启用了任何传感器,您必须处理ISensorEvents::OnStateChanged 事件。
// Get the sensor collection. hr = pSensorManager->GetSensorsByType(SAMPLE_SENSOR_TYPE_TIME, &pSensorColl); if(SUCCEEDED(hr)) { // Request permissions for all sensors // in the collection. hr = pSensorManager->RequestPermissions(0, pSensorColl, FALSE); }您可以选择在尝试检索数据之前以同步方式测试传感器状态。下面的示例代码演示此方法。
if(SUCCEEDED(hr)) { // Check the current sensor state. SensorState state = SENSOR_STATE_NOT_AVAILABLE; hr = pSensor->GetState(&state); if(SUCCEEDED(hr)) { if(state == SENSOR_STATE_ACCESS_DENIED) { wprintf_s(L"\nSensor not enabled, requesting permissions...\n"); hr = pSensorManager->RequestPermissions(0, pSensorColl, TRUE); if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) || hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { wprintf_s(L"\nYou have previously denied access to this sensor.\n"); wprintf_s(L"Please use the Location and Other Sensors control panel\n"); wprintf_s(L"to enable the WDK Time Sensor and run this program again.\n"); } } } } if(SUCCEEDED(hr)) { // Get the data report. hr = pSensor->GetData(&pReport); }
下面的示例代码将在尝试从特定传感器检索数据报告失败的情况下提示用户许可使用传感器。
if(SUCCEEDED(hr)) { // Get the data report. hr = pSensor->GetData(&pReport); if(E_ACCESSDENIED == hr) { wprintf_s(L"\nSensor not enabled, requesting permissions...\n"); hr = pSensorManager->RequestPermissions(0, pSensorColl, TRUE); if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) || hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { wprintf_s(L"\nYou have previously denied access to this sensor.\n"); wprintf_s(L"Please use the Location and Other Sensors control panel\n"); wprintf_s(L"to enable the WDK Time Sensor and run this program again.\n"); } } }
相关文章推荐
- openSUSE系统下VirtualBox无法挂载主机USB设备问题的解决
- 关于编译报错“dereferencing pointer to incomplete type...
- iOS 9 适配
- POJ 3614 Sun Bathing (贪心)
- 使用Jedis连接集群Redis(支持Redis复杂操作)
- 飞思卡尔GPIO学习
- angularjs2.0 五分钟入门教程之typescript版本
- 【Python之旅】第三篇(四):Pythoh模块
- 【Python之旅】第三篇(四):Pythoh模块
- UML
- Saltstack 安装和配置
- HDU 5441 Travel(最短路径变形)
- 说说Makefile那些事儿
- checkbox全选和取消全选
- 有名管道
- 硬盘大于2T以上,用parted创建分区安装centos6.5
- 统计英文单词词频
- 日经春秋 20150924
- MSDN ATL教程链接,好好学习,天天向上
- C#Winform定时执行一段程序