- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在实现 SensorEventListener 的类中出现 OutOfMemoryError。在日志中有时我会遇到 OutOfMemoryError
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
gravity = event.values;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
geomagnetic = event.values;
}
if (gravity != null && geomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuthInRadians = orientation[0];
float azimuthInDegress = (float)Math.toDegrees(azimuthInRadians);
if (azimuthInDegress < 0.0f) {
azimuthInDegress += 360.0f;
}
azimuth = (int) azimuthInDegress;
Hawk.put(HawkConst.AZIMUTH, azimuth);
}
}
}
这是我的日志。有时我会遇到此错误,但并非总是如此
at com.android.internal.util.FastXmlSerializer.<init>(FastXmlSerializer.java:55)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:177)
at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:596)
at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:52)
at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:511)
at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:532)
at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:52)
at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:454)
at com.orhanobut.hawk.SharedPreferencesStorage.put(SharedPreferencesStorage.java:23)
at com.orhanobut.hawk.Hawk.put(Hawk.java:63)
at pl.***.****.worker.Tracker.onSensorChanged(Tracker.java:154)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:474)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:131)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(NativeStart.java)
最佳答案
您似乎在 onSensorChanged() 回调中做了一些繁重的工作。因此,在您的回调中,您正在调用 SharedPreferences。它不好的原因是每次传感器有新值时都会调用 onSensorChanged() ,根据您的监听器配置,这可能会每秒发生多次。这意味着您尝试每秒多次保存到文件(使用共享首选项)。这需要在 andorid 方面进行大量分配,并可能导致 OutOfMemoryError。
为了解决这个问题,我建议将值存储在变量中,并且仅以恒定的时间间隔或基于某些事件(按钮单击、生命周期事件等)执行保存。
我还看到您正在使用 .commit() ,它会阻塞线程直到保存文件,您可以尝试使用 .apply() ,它将提交操作移动到其他线程。不管怎样,您都需要限制使用共享首选项的次数。
关于Java android SensorEventListener 中的 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452348/
我是 Android 的新手,我正在尝试编写一个应用程序,通过网络将传感器数据从手机发送到 PC。 我知道我不应该在 UI 线程上执行“昂贵”的操作。 Android 允许您使用 Handler 注册
我正在尝试实现 SensorEventListener,但由于某种原因什么也没发生。我厌倦了为听众创建一个单独的类,但它仍然无法正常工作。该服务在单独的线程中运行。(在 list android:pr
我已经准备好被羞辱了,但我不确定哪里出了问题。我只是在玩 Android 开发,只是制作一个简单的应用程序,将加速度计打印到 TextView。 创建 SensorEventListener 时,出现
这似乎是一个基本问题,但在搜索了一段时间并使用它之后,我已经到了需要帮助的地步。我想让 SensorEventListener 在与 UI 不同的线程中运行,这样当事件进入时需要进行的计算不会减慢 U
我想在检测到摇动事件时启动应用程序的 Activity 。我希望这发生在我的应用程序之外。 有很多很棒的代码 fragment here和 here例如,如何正确实现SensorEventListen
我正在尝试编写一个实现 SensorEventListener 的类来确定手机摄像头朝向 (Galaxy S3) 的方向,以磁北以东的度数表示。以下是我使用的传感器: public void st
我正在尝试向我的应用程序中的 fragment 添加一个传感器事件监听器。 下面是我用来定义传感器的代码 Context context; @Override public View onCreate
我在实现 SensorEventListener 的类中出现 OutOfMemoryError。在日志中有时我会遇到 OutOfMemoryError @Override public void on
我正在尝试提供一个后台服务来读取手机的方位角、俯仰角和横滚角,并通过网络发送它们。我已经创建了一个 IntentService 和计算代码来计算方向,但是我不太确定如何将后台服务注册为事件监听器。 p
我对 Android 中的 IntentService 有疑问。我定义自己的服务如下: public class ABC extends IntentService implements Sensor
我正在创建一个名为 CompassApp 的应用程序。我创建了两个名为 Compass.java 和 Rose.java 的类。此应用程序是根据网站中描述的教程创建的,并且使用了已弃用的 Sensor
我好像和这位先生有同样的问题: SensorEventListener doesn't get unregistered with unregisterListener() method public
我有一个非常简单的 Android 应用程序:在 Activity 中我有一个按钮,我启动/停止 OrientationListener。但是,注销后,在 ddms 中我仍然可以看到线程 androi
我正在创建一个用于测试目的的 compass 应用程序,它工作得很好,箭头图像指向北极,但它变化太大,即使设备没有移动,也会在几毫秒内左右做出非常小的移动。 我的问题是如何忽略这些小变化?谢谢 pub
我正在尝试制作一个应用来以度数显示手机旋转,但没有调用 onSensorChanged。 textView 中的文本没有改变,我不知道为什么。它不会给出任何编译错误。这是代码。 package com
我正在尝试将传感器测量值记录到设备内存中。为此,我为许多传感器注册了相同的 SensorEventListener,然后根据类型使用开关将它们分开。 例如。 int type = sensor.get
我正在做一个项目,我想在设备移动时拍照,以创建一个全景图像本身。一旦陀螺力超过阈值,我希望相机拍照。我有故障保险柜,所以一次只能拍摄一张照片,但我不知道为什么会发生故障。 这是我的主要 Activit
我正在尝试创建一个自定义 View ,其中位图会随着手机的移动而移动。 所以我创建了自定义 View 并实现了 sensorEventListener: public class MovingStar
更新: 我能够通过引入类范围静态计数器并忽略 x 数量的事件来解决我遇到的特定问题。但我仍然想知道我做错了什么:以微秒为单位用提示注册监听器,而不是使用四个给定常量之一。 我的应用程序中的一个 Act
我是一名优秀的程序员,十分优秀!