-1

For example, it's possible to press a button in your app and return current accelerometer values?

Or it's possible to get current device orientation (no screen orientation) pressing a button (without persistent listeners)? Thanks!

user2383054
  • 175
  • 1
  • 1
  • 10
  • listeners have to react on something, either for example onscreenchange, onbuttonclick, ongpssignallocationchange, etc.. So, as far as I understand, on any event, sensors are being queried either by application, services, whatever. So to answer your question, then you will be able to get current status of sensors by using for example onClickListener (accelerometer values | orientation | gps_location | etc) – Boris Mocialov Jul 01 '13 at 18:54
  • Thanks, but i want to know about how get the sensor data, this is my problem. – user2383054 Jul 01 '13 at 19:08
  • Which sensor exactly are you interested in? – Boris Mocialov Jul 01 '13 at 19:19
  • I want to get current device orientation, so i think that accelerometer. I don't know if another type sensor is involved. :) – user2383054 Jul 01 '13 at 19:22
  • check this out: http://stackoverflow.com/a/6012007/1276374 – Boris Mocialov Jul 01 '13 at 19:23

1 Answers1

0

Yes that's possible. Just give the corresponding function to the OnClick event of the button that you want to use.

Maybe this tutorial can help you as a starting point.

Edit: Here is an example of how to do it.

First create a button in the layout XML:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

Then, in the main activity write the following lines in onCreate:

final Button myButton= (Button) findViewById(R.id.button1);
myButton.setOnClickListener(new OnClickListener() 
    {   public void onClick(View v) 
        {   
            // What goes here will be executed after clicking the button
        }
    });

You need to implement the sensor listener in order to keep a fresh value of the sensors and everytime it changes save a copy into a global variable. Then when clicking the button just call the last value stored.

For orientation you can use the compass instead of the accelerometer but it depends on what you want to do.

Julian Mancera
  • 194
  • 1
  • 12
  • THanks, but you know anything about this onCLick function? Can you write an example? This is my question. – user2383054 Jul 01 '13 at 19:01
  • Thanks, then a sensor listener is always required right? – user2383054 Jul 02 '13 at 16:54
  • 1
    Yes, the sensor listener is the way Android uses to get access to the sensors. You can get more information about this topic [here](http://developer.android.com/reference/android/hardware/SensorManager.html) – Julian Mancera Jul 02 '13 at 17:07