Using an Android phone's sensors
Accessing the sensor data of a phone, is not too complicated, but since API level 3, a lot of things got deprecated. Using only the emulator, you have only a few (here is one) options, to simulate sensor data. I would recommend the use of a physical phone. Keep in mind, that not all phones have the same sensors integrated! Cheaper phones might not have a temperature sensor, or a gyroscope, but I'm pretty sure, that all Android phones have at least an accelerometer, and an orientation sensor.
Here is an example, how to access acceleration and orientation sensor data (in API level 3 and above), without using any deprecated methods.
- public class SensorTest extends Activity implements SensorEventListener {
- SensorManager sensorManager = null;
- //for accelerometer values
- TextView outputX;
- TextView outputY;
- TextView outputZ;
- //for orientation values
- TextView outputX2;
- TextView outputY2;
- TextView outputZ2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
Start with sensorManager setup
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- setContentView(R.layout.main);
- //just some textviews, for data output
- outputX = (TextView) findViewById(R.id.TextView01);
- outputY = (TextView) findViewById(R.id.TextView02);
- outputZ = (TextView) findViewById(R.id.TextView03);
- outputX2 = (TextView) findViewById(R.id.TextView04);
- outputY2 = (TextView) findViewById(R.id.TextView05);
- outputZ2 = (TextView) findViewById(R.id.TextView06);
- }
Now register the required listeners, it is recommended, to put this part in onResume.
What's interesting here, is the refresh speed. Just like using a toast(short,long...), there is no easy way, to set up a proper time limit. There are 4 sensor delay settings: fastest, game, normal, and ui.
Try them all out, and check which one works the best for your application. Aim for the lowest required refresh speed!
- @Override
- protected void onResume() {
- super.onResume();
- sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
- sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME);
- }
It is recommended, to stop using the sensor event listener, as soon as you don't need it, and it should not be left running in the background. It can drain the battery pretty fast.
- @Override
- protected void onStop() {
- super.onStop();
- sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
- sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
- }
The event.sensor.getType() will tell us, what kind of event just happened.
- public void onSensorChanged(SensorEvent event) {
- synchronized (this) {
- switch (event.sensor.getType()){
- case Sensor.TYPE_ACCELEROMETER:
- break;
- case Sensor.TYPE_ORIENTATION:
- break;
- }
- }
- }
There is also an onAccuracyChanged part, which we don't care about right now.
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {}
- }
To sum things up, to access sensor data you have to do the following things:
1. Check sensor availability.
2. Register a listener to a sensorManager.
3. Catch the needed data , from onSensorChanged.
4. Unregister the sensorManager's listener.
You can read more, about those seemingly random numbers on the above screenshot, in the official google documentation of Android here.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
12 years 4 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
12 years 4 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
12 years 4 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
12 years 4 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
12 years 4 weeks ago
Poll
Useful resources
Android Development Projects
- App Development
- Telegram bulk messages sender
- Photo Editing Application
- React Native Developer - part time basis
- Locket App Copy
- Courier app $150 only
- Mobile Application Development - IOS and Android (Rewards for sharing brand ads on social media)
- Android app for sms
- Fitness app project for sale online
- io developer