Android 传感器 CPU 使用率

发布于 2024-12-17 05:54:57 字数 1380 浏览 1 评论 0原文

我正在尝试从 Samsung Galaxy Tab GT-P1000 读取多个传感器,相对于我使用的应用程序,它们似乎非常严重地占用 CPU。

作为测试,我创建了一个简短的程序,它实现了加速度计传感器的 SensorEventListener,但对传感器读数不执行任何操作:

public class SensorTestActivity extends Activity implements SensorEventListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SensorManager oSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        oSensorManager.registerListener(this, oSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

    }
}

这会导致我在调试时持续 10% 的 CPU 使用率(即我的设备已插入 PC)和 5% 的使用率,而我却没有。如果我使用 SENSOR_DELAY_FASTEST,则在调试时使用率会飙升至恒定 30%,而在调试时则使用率飙升至恒定 20%。

当我想使用多个传感器时,这会产生一个大问题,因为它们的 CPU 使用率很高,而且没有任何数据处理。我使用过 Android Market 中的 Compass 应用程序,但没有一个在给定时间使用超过 5% 的 CPU,所以我觉得我错过了一些明显的东西,但找不到其他人有同样的问题。

我没有编辑此应用程序的清单文件或布局 - 它是 Eclipse 制作的默认模板,并且我添加了传感器。

更新:我读取 CPU 使用率的方法有缺陷,因为我使用任务管理器来测量它。当任务管理器打开时,我的应用程序不会使用 onPause 停止传感器,而大多数其他应用程序会这样做。

I am trying to read multiple sensors from a Samsung Galaxy Tab GT-P1000 and they seem to be hogging the CPU quite badly relative to the applications I've used.

As a test, I created a short program which implements the SensorEventListener for the Accelerometer sensor, but does nothing with the sensor readings:

public class SensorTestActivity extends Activity implements SensorEventListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SensorManager oSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        oSensorManager.registerListener(this, oSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

    }
}

This results in 10% constant CPU usage while I'm debugging (ie my device is plugged in to my PC) and 5% usage while I'm not. If I use SENSOR_DELAY_FASTEST, the usage skyrockets to constant 30% while I'm debugging and 20% while I'm not.

This creates a massive problem when I want to use multiple sensors, because they all have high CPU usage and that's without any data processing. I've used Compass applications from the Android Market and none of them use more than 5% of the CPU at a given time, so I feel like I'm missing something blatantly obvious, but can't find anyone else having the same problem.

I've not edited the manifest file or layout for this application - it's the default template made by Eclipse and I've added the Sensor.

UPDATE: My method of reading the CPU usage is flawed, because I was using the task manager to measure it. My application isn't stopping the sensors using onPause when the task manager opens, whereas most other applications would do so.

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

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

发布评论

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

评论(1

久随 2024-12-24 05:54:57

这会导致我在调试时(即我的设备已插入 PC)CPU 使用率恒定为 10%,而在调试时则为 5%。如果我使用 SENSOR_DELAY_FASTEST,则在调试时使用率会飙升至恒定 30%,而在调试时则使用率飙升至恒定 20%。

然后使用 SENSOR_DELAY_UISENSOR_DELAY_NORMAL,这两种方法更新传感器数据的频率都较低,因此消耗的 CPU 时间也相应较少。

我使用过 Android Market 中的 Compass 应用程序,它们在给定时间使用的 CPU 都不超过 5%

它们可能不使用 SENSOR_DELAY_GAMESENSOR_DELAY_FASTEST

除此之外,不同的设备(可能具有不同的传感器芯片组)可能会导致不同的 CPU 利用率。例如,某些设备在 LogCat 中非常烦人——基于传感器读数进行记录的设备会相应地变慢。

另外,我不知道你如何测量 CPU 利用率,但我不知道有任何官方支持的方法来做到这一点。我会重点关注游戏中的帧速率或您正在编写的任何内容。

This results in 10% constant CPU usage while I'm debugging (ie my device is plugged in to my PC) and 5% usage while I'm not. If I use SENSOR_DELAY_FASTEST, the usage skyrockets to constant 30% while I'm debugging and 20% while I'm not.

Then use SENSOR_DELAY_UI or SENSOR_DELAY_NORMAL, both of which update you with sensor data less frequently and should consume correspondingly less CPU time.

I've used Compass applications from the Android Market and none of them use more than 5% of the CPU at a given time

They probably don't use SENSOR_DELAY_GAME or SENSOR_DELAY_FASTEST.

Beyond that, different devices (perhaps with different sensor chipsets) may result in different CPU utilization. For example, some devices are annoyingly chatty in LogCat -- devices that log based on sensor readings would be commensurately slower.

Also, I don't know how you are measuring CPU utilization, but I'm not aware of any officially supported means for doing that. I'd focus instead on things like frame rate in your game or whatever it is that you are writing.

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