访问 POSIX-timer 事件处理程序中的 TSensorManager.Current 时出错
Error accessing to the TSensorManager.Current in the POSIX-timer event handler
Uses AndroidApi.Timer, System.Sensors;
我不想通过 GPS 传感器给电池放电,因为在我的 Adnroid 服务中我使用了 POSIX-timer (AndroidTimerCreate)。在它的事件处理程序中,我不时(每 5 分钟)启用和禁用 LocationSensor。
当我参考属性 TSensorManager.Current:
TSensorManager.Current.Activate;
FSensors := SensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);
我收到错误:
Сan't start sensor: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
但是如果在AndroidServiceStartCommand或AndroidServiceCreate方法中执行这段代码,那么SensorManager很好激活。
为了测试,我将代码从服务转移到多设备应用程序——问题仍然存在。
请帮助我解决问题,如何启用和禁用计时器中的 LocationSensor(enable/disable 每 5 分钟一次)?
我找到了解决方案:
- 在操作 ACTION_LOCATION_CHANGE 上为 LocalBroadcastManager 创建过滤器,注册 LocalBroadcastReceiver;
- 在计时器的事件中发送 sendBroadcast 动作 ACTION_LOCATION_CHANGE;
- 在handler中LocalBroadcastReceiver切换TLocationSensor's属性Active.
我发现了 the sulution here,很好的例子。
Uses AndroidApi.Timer, System.Sensors;
我不想通过 GPS 传感器给电池放电,因为在我的 Adnroid 服务中我使用了 POSIX-timer (AndroidTimerCreate)。在它的事件处理程序中,我不时(每 5 分钟)启用和禁用 LocationSensor。
当我参考属性 TSensorManager.Current:
TSensorManager.Current.Activate;
FSensors := SensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);
我收到错误:
Сan't start sensor: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
但是如果在AndroidServiceStartCommand或AndroidServiceCreate方法中执行这段代码,那么SensorManager很好激活。
为了测试,我将代码从服务转移到多设备应用程序——问题仍然存在。
请帮助我解决问题,如何启用和禁用计时器中的 LocationSensor(enable/disable 每 5 分钟一次)?
我找到了解决方案:
- 在操作 ACTION_LOCATION_CHANGE 上为 LocalBroadcastManager 创建过滤器,注册 LocalBroadcastReceiver;
- 在计时器的事件中发送 sendBroadcast 动作 ACTION_LOCATION_CHANGE;
- 在handler中LocalBroadcastReceiver切换TLocationSensor's属性Active.
我发现了 the sulution here,很好的例子。