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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With