Currently, Android mobile phones released on the market have comprehensively improved the Android compass, camera preview, accelerometer, and GPS subsystems, the following article will give a comprehensive introduction to its functions.
Obtaining the location manager seems simple, but you still have to remember some things. First, we may only request the location manager in the main UI thread. We either request the LocationManager object in the onCreate call of the action.
You can use LocationManager to create an executable object running in the main thread. For simplicity, the following sample code registers the LocationManager update from the onCreate method of an action. As you can see, a LocationManager object is declared here ..
Use getSystemService to obtain your object and then call requestLocationUpdates. You may want to know which parameters are required when the location is updated. First, you tell the system that you want to use the location update function of the GPS device in the system. Then, tell it how long you want to update (in this example, the interval is 100 ms ).
It is updated whenever it is moved more than one meter. In this way, Android phones can quickly identify their movements and adjust their relationships with other objects. Finally, input an instance of the class that implements the LocationListener interface. After a request is sent for location update, the LocationListener class receives the initial location and changes the location later. Here is our LocationListener:
- <Html>
-
- <Head>
-
- <Title>PageTitle</Title>
-
- </Head>
-
- <Body>
-
- Thecontentofthebodyelement.
-
- </Body>
-
- </Html>
-
- Just like the XML layout of Android, all elements are structured and can be expressed in a tree structure:
-
- <?XmlversionXmlversion= "1.0" encoding = "UTF-8 ″?>
-
- <LinearLayout
-
- Http://schemas.android.com/apk/res/android
-
- Android: orientation = "vertical"
-
- Android: layout_width = "fill_parent"
-
- Android: layout_height = "fill_parent">
-
- <TextView
-
- Android: layout_width = "wrap_content"
-
- Android: layout_height = "wrap_content"
-
- Android: text = "HelloWorld"/>
-
- </LinearLayout>
In the code above, the onLocationChanged method is the only thing we need to care about. However, we will introduce other methods of this object. So that you can understand this object when copying it to your own code. Once the satellite locks the device, the method onLocationChanged will be called, and each time after the specified interval (100 ms in this example) in request updating, it will be called once.
Each time a Location is updated, a Location object is generated. Through this class, we can obtain the longitude and latitude of the target and accomplish many important tasks. Here we are most interested in getLatitude (), getlongdistance (), bearingTo () and distanceTo (). Using these four functions, we can calculate the azimuth of any subsequent positions and determine the distance from you.
- sensorMan = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
- sensorMan.registerListener(listener,
- sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
- SensorManager.SENSOR_DELAY_FASTEST);
We call the getSystemService method of the Android mobile phone context object (ctx in the above Code. The following is the complete code for the direction listener and the acceleration sensor listener.