Use sensors and LBS (1) In OPhone applications)

Source: Internet
Author: User

The OPhone platform has many built-in sensors. With the latest hardware technology and powerful system APIs, developers can easily call the built-in sensors of mobile phones, write special apps that are suitable for mobile devices and provide users with a better mobile app experience.
This article takes the most common gravity sensor and position sensor as an example to describe how to develop sensor-based applications on the OPhone platform.

Build an OPhone Development Environment

To develop OPhone applications, we need to first build an OPhone development environment. Currently, the OPhone development platform supports Windows and Linux. You can refer to the article "setting up an OPhone development environment for RSS Reader instance development" to set up an OPhone development environment.

Use gravity Sensor

Gravity Sensors are the most commonly used sensors in OPhone applications. They are used to detect the mobile phone's tilt angle in all directions. Gravity Sensors have sensing data in three directions: X, Y, and Z. the X, Y, and Z axes are defined as follows:

 

When the mobile phone is shaking along a certain axis, the sensor returns a value between-9.81 and + 9.81, indicating the tilt angle of the mobile phone. It can be understood as the acceleration of gravity when a ball is placed in the current position of the mobile phone. When the mobile phone is placed horizontally, the gravity acceleration of the ball is 0:

 

When the mobile phone is placed vertically, the gravity acceleration of the ball is 9.81:

 

When the mobile phone is placed with a certain tilt angle, the gravity acceleration of the ball is between 0 and 9.81:

 


The actual return value is-9.81 to + 9.81. Positive and Negative are used to distinguish the two directions of the mobile phone. The Rotation Position of the current mobile phone can be completely determined by the gravity acceleration returned by the three axes X, Y, and Z.
Gravity Sensors are vital to the development of sensor games. Due to restrictions on mobile phone buttons, it is not easy to use the keyboard's upper and lower keys to control the game, while gravity sensors allow players to shake their mobile phones to control the game, brings a better and more distinctive gaming experience. The famous mobile game "balancer" relies entirely on Gravity Sensors to allow gamers to control the game:

 


Next, we will use a practical example to demonstrate how to use a gravity sensor. This simplified "balancer" example will draw a ball in the center of the mobile phone screen. When the user shakes the mobile phone, the ball will move up, down, and left and right.

  • How to get started with Ophone Widget SDK
  • Quick Start: getting started with Ophone and Android
  • How to port a j2-api to OPhone
  • Combat between Android platform and OPhone
  • Explanation of Native Development and JNI mechanism on OPhone Platform

Create an OPhone project named Accelerometer and edit the XML layout. We place a TextView in LinearLayout to display the value returned by the gravity sensor. A FrameLayout contains a custom BallView. The content is as follows:

 
 
  1. xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6. >      
  7.     <TextView xmlns:android=  
  8. "http://schemas.android.com/apk/res/android"    
  9.         android:id="@+android:id/ball_prompt"    
  10.         android:layout_width="fill_parent"    
  11.         android:layout_height="wrap_content"    
  12.         android:text="Sensor: 0, 0, 0"    
  13.     />     
  14.     <FrameLayout xmlns:android=  
  15. "http://schemas.android.com/apk/res/android"    
  16.         android:id="@+android:id/ball_container"    
  17.         android:layout_width="fill_parent"    
  18.         android:layout_height="fill_parent"    
  19.     >     
  20.         <org.expressme.wireless.accelerometer.BallView xmlns:android=  
  21. "http://schemas.android.com/apk/res/android"    
  22.             android:id="@+android:id/ball"    
  23.             android:src="@drawable/icon"    
  24.             android:scaleType="center"    
  25.             android:layout_width="wrap_content"    
  26.             android:layout_height="wrap_content"    
  27.         />     
  28.      FrameLayout>     

BallView is derived from ImageView. It uses the moveTo (x, y) method to easily move a ball to a specified position:

 
 
  1. public class BallView extends ImageView {     
  2.     public BallView(Context context) {     
  3.         super(context);     
  4.     }     
  5.       
  6.     public BallView(Context context, AttributeSet attrs) {     
  7.         super(context, attrs);     
  8.     }     
  9.       
  10.     public BallView(Context context, AttributeSet attrs, int defStyle) {     
  11.         super(context, attrs, defStyle);     
  12.     }     
  13.       
  14.     public void moveTo(int l, int t) {     
  15.         super.setFrame(l, t, l + getWidth(), t + getHeight());     
  16.     }     
  17. }    

Next, we need to write the main logic in MainActivity to obtain the value returned by the gravity sensor. Gravity Sensor APIs are mainly SensorManager and Sensor. In the onCreate () method of Activity, we can obtain the system's SensorManager instance, and then obtain the corresponding Sensor instance:

 
 
  1. public class MainActivity extends Activity {     
  2.     SensorManager sensorManager = null;     
  3.     Sensor sensor = null;     
  4.       
  5.     @Override    
  6.     public void onCreate(Bundle savedInstanceState) {     
  7.         super.onCreate(savedInstanceState);     
  8.         setContentView(R.layout.main);     
  9.         prompt = (TextView) findViewById(R.id.ball_prompt);     
  10.         sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);     
  11.         sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);     
  12.     }     
  13.     ...     
  14. }    

We compile a register () method to register SensorEventListener with SensorManager, and then respond to the onSensorChanged event in SensorEventListener and process it. The unregister () method cancels the registered SensorEventListener.

The onSensorChanged event of SensorEventListener will return the SensorEvent object, which contains the latest Sensor data, and obtain a float [] array through e. values. For different Sensor types, the number of elements in the array is different. The gravity Sensor always returns an array with a length of 3, representing the values in the X, Y, and Z directions, respectively. The Z axis indicates whether the mobile phone is screen-up or screen-down. It is not commonly used. We mainly care about the values of the x axis and y axis to locate the position of the ball on the screen through (x, y. The X and Y values of the gravity sensor can easily locate the position of the ball:

 
 
  1. public class MainActivity extends Activity {     
  2.     private static final float MAX_ACCELEROMETER = 9.81f;     
  3.       
  4.     // x, y is between [-MAX_ACCELEROMETER, MAX_ACCELEROMETER]     
  5.     void moveTo(float x, float y) {     
  6.         int max_x = (container_width - ball_width) / 2;     
  7.         int max_y = (container_height - ball_height) / 2;     
  8.         int pixel_x = (int) (max_x * x / MAX_ACCELEROMETER + 0.5);     
  9.         int pixel_y = (int) (max_y * y / MAX_ACCELEROMETER + 0.5);     
  10.         translate(pixel_x, pixel_y);     
  11.     }     
  12.       
  13.     void translate(int pixelX, int pixelY) {     
  14.         int x = pixelX + container_width / 2 - ball_width / 2;     
  15.         int y = pixelY + container_height / 2 - ball_height / 2;     
  16.         ball.moveTo(x, y);     
  17.     }     
  18. }    

Call getDefaultSensor () of SensorManager to obtain the Sensor instance. Here, we pass in Sensor. TYPE_ACCELEROMETER to obtain the instance of the gravity Sensor.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.