PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
69 Posts
Hardware
123 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
20 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


Coil Winding machine counter with Atmega8 and Reed relay | 72 Views | Rate 72
Coil Winding machine counter with Atmega8 and Reed relay
uRADMonitor - Online Radiation monitoring station | 15057 Views | Rate 69.71
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18699 Views | Rate 59.74
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4937 Views | Rate 55.47
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 5188 Views | Rate 45.11
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4766 Views | Rate 44.54
Building a robot – Part 2
Simple Switched power Supplies | 16439 Views | Rate 42.26
Simple Switched power Supplies
Tube: GP-5 (ГП-5) | 40 Views | Rate 40
Tube: GP-5 (ГП-5)

 
  

Android default orientation and orientation change events


By Radu Motisan Posted on July 24th, 2012 , 2436 Views (Rate 7.99)



  




There are times when you want to lock your Activity in Landscape only or Portrait only modes (using AndroidManifest's

  1. <activity ... android:screenOrientation="portrait"/>

clause or directly from code using

  1. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

).
But parts of your interface might need to update based on orientation changes (user rotates the device).
The problem is that having your Activity orientation locked, you won't receive the OnOrientationChanged notification.

To solve this issue you will need to do two things:
1. register for sensor notifications, in this case we'll need the Sensor.TYPE_ORIENTATION
2. know the device natural orientation (for tablets it is landscape orientation, for phones it is portrait), because the coordinate-system returned by the sensors, is defined relative to the screen of the phone in its default orientation

Here's how to code it:
1. In your Activity class define the following variables:

  1.  
  2. //sensors
  3. private SensorManager sensorManager;
  4. private static final int sensor = SensorManager.SENSOR_ORIENTATION;
  5. private Sensor mySensor;
  6. int m_nOrientation = -1; // invalid default value
  7.  

2. In your Activity's OnCreate setup your sensor variables :

  1.  
  2. // get sensor manager
  3. sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  4. // get sensor
  5. mySensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  6.  

3. In your Activity's OnResume register for sensor notifications, and in OnPause remove them:

  1.  
  2. @Override public void onResume() {
  3. super.onResume();
  4. if (sensorManager!= null)
  5. sensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
  6.  
  1.  
  2. @Override public void onPause() {
  3. super.onPause();
  4. if (sensorManager != null)
  5. sensorManager.unregisterListener(this);
  6. }
  7.  

4. Make the main Activity class implement SensorEventListener, and add the interface methods onAccuracyChanged and onSensorChanged. Add the following code to onSensorChanged:

  1.  
  2. @Override
  3. public void onSensorChanged(SensorEvent event) {
  4. // TODO Auto-generated method stub
  5. if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
  6. //The coordinate-system is defined relative to the screen of the phone in its default orientation
  7. int orientation = 0;
  8. float roll=0;
  9. float pitch=0;
  10. switch (getWindowManager().getDefaultDisplay().getRotation()) {
  11. case Surface.ROTATION_0:
  12. roll=event.values[2];
  13. pitch=event.values[1];
  14. break;
  15. case Surface.ROTATION_90:
  16. roll=event.values[1];
  17. pitch=-event.values[2];
  18. break;
  19. case Surface.ROTATION_180:
  20. roll=-event.values[2];
  21. pitch=-event.values[1];
  22. break;
  23. case Surface.ROTATION_270:
  24. roll=-event.values[1];
  25. pitch=event.values[2];
  26. break;
  27. }
  28. if (pitch >= -45 && pitch < 45 && roll >= 45) orientation = 0;
  29. else if (pitch < -45 && roll >= -45 && roll < 45) orientation = 1;
  30. else if (pitch >= -45 && pitch < 45 && roll < -45) orientation = 2;
  31. else if (pitch >= 45 && roll >= -45 && roll < 45 ) orientation = 3;
  32.  
  33. if (m_nOrientation != orientation) { //orientation changed event
  34. m_Inst.Debug(LOG_TAG,"onSensorChanged: orientation:" + orientation);
  35. m_nOrientation = orientation;
  36. // fire event for new notification, or update your interface here
  37. }
  38. }
  39.  

Thanks go to diyism, on stackoverflow.






  

More on PocketMagic:

Programmatically Injecting Events on Android - Part 1 | 10525 Views | Rate 26.92
Programmatically Injecting Events on Android - Part 1
Atmega8 and enc28J60 for ethernet support | 7502 Views | Rate 25.09
Atmega8 and enc28J60 for ethernet support
Tube: USN-5J29 | 419 Views | Rate 24.65
Tube: USN-5J29
How to set the AVR Fusebits | 1793 Views | Rate 23.91
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2133 Views | Rate 22.94
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Dual H-Bridge for controlling two motors | 1302 Views | Rate 22.07
Dual H-Bridge for controlling two motors

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string gCz4YR to the field below: