如何在屏幕方向改变期间处理传感器监控?

发布于 2024-12-28 09:30:24 字数 816 浏览 2 评论 0 原文

在我的活动中,我使用“onSensorChanged”方法监视传感器。

当按下该活动中的按钮时(在“onClick”方法中),我开始监视传感器。

我在 onPause 和 onResume 方法中注册和取消注册监听。

这是代码:

public void onCreate(Bundle savedInstanceState) {
  //Here i set "mySensor"
}

public void onClick(View view){
  sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}

protected void onPause() {
  sensorManager.unregisterListener(this, mySensor);
}

protected void onResume() {
  sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}

public final void onSensorChanged(SensorEvent event) {
  //do things
}

问题是,当屏幕方向发生变化时,监视停止,我在“onSensorChanged”中执行的操作停止,直到我再次按下按钮。

我一直在阅读,似乎当屏幕方向改变时,我的活动被破坏并重新创建,但我希望传感器的监控在此过程中不会停止。

如何保持监控?我认为必须使用捆绑包来完成,但我不确定。

谢谢。

In my activity, i monitor a sensor, with "onSensorChanged" method.

I start monitoring the sensor when a button in that activity is pushed (in the "onClick" method).

I register and unregister the listening in the onPause and onResume methods.

Here is the code:

public void onCreate(Bundle savedInstanceState) {
  //Here i set "mySensor"
}

public void onClick(View view){
  sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}

protected void onPause() {
  sensorManager.unregisterListener(this, mySensor);
}

protected void onResume() {
  sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_FASTEST);
}

public final void onSensorChanged(SensorEvent event) {
  //do things
}

The problem is that, when there is an screen orientation change, the monitoring stops, the things i do within "onSensorChanged" stop until i press the button again.

I have been reading, and it seems that when the screen orientation changes, my activity is destroyed and recreated, but i would like the monitoring of the sensor not stopping during that process.

How can i keep the monitoring? i think it must be done using the Bundle, but im not sure.

Thanx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2025-01-04 09:30:24

您有几个选择:

  1. 您可以将传感器侦听器移动到应用程序类中,该应用程序类由清单中 中的 android:name 定义。请参阅此处,命名您派生的类来自 android.app.Application。我推荐这种方法。

  2. 您可以将android:configChanges="orientation"添加到您的活动清单中,但是,如此处,您需要在 onConfigurationChanged() 中处理适当资源的重新加载,例如特定于方向的布局你的活动。我不建议这样做,因为当其他配置更改发生时,您仍然会遇到监视消失的问题,或者您需要弄清楚应用程序应如何处理它们(并将它们添加到 android 中的更改列表中) :configChanges)。

  3. 按照 此处,但我真的不会推荐这样做,除非有其他原因导致您希望应用程序仅在特定方向上工作。

You have a few choices:

  1. You can move your sensor listener into your application class, defined by android:name in <application> in your manifest. See here, naming a class that you've derived from android.app.Application. I would recommend this approach.

  2. You can add android:configChanges="orientation" to your activity manifest but, as described here, you'll need to handle the reloading of appropriate resources, such as orientation specific layout, in the onConfigurationChanged() of your activity. I wouldn't recommend this as you would still have the issue of the monitoring disappearing when the other configuration changes occur or you will need to work out how your application should handle them (and add them to the list of changes in android:configChanges).

  3. Disabled orientation changes entirely for your application by using android:screenOrientation as described here, but I really wouldn't recommend this unless there are other reasons why you want your application to only work in a certain orientation.

勿忘心安 2025-01-04 09:30:24

事实上,当设备方向改变时,Android 上的 Activity 确实会被销毁并重新创建。如果您不关心所有方向,您可以将您的活动“限制”为单一方向。

或者,如果您希望提供对多个方向(默认)的支持,您可以将传感器读取部分移动到静态单例(您可以从您想要的任何 Activity 中调用)或 Application 类的子类中,或者一个单独的类,由 Application 类的子类引用),您可以使用 getApplication() 调用它 - 阅读 this)。一般来说,这三种选择对于在活动中保持全局状态来说都是相当不错的选择。

如果您希望在应用程序处于后台或应用程序终止后仍继续读取传感器数据,则一种更可靠的方法是使用服务并在应用程序启动时与您的活动来回传递数据。跑步。

Indeed, Activities on Android do get destroyed and recreated when the orientation of the device changes. If you do not care for all orientations, you can 'limit' your activity a single orientation.

Alternatively, if you wish to provide support for more than one orientation (the default), you can move the sensor reading part either into a static singleton (that you would call from whichever Activity you wanted), into a subclass of the Application class, or a separate class, referenced by a subclass of the Application class) that you could call with getApplication() — read this for more information about the Application class). All three alternatives are pretty decent choices for keeping global state across Activities in general.

A somewhat more robust way to do it, if you wish to keep reading sensor data even when the application is in the background or after your application is terminated, is to use a Service and pass data back and forth with your activities when the app is running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文