All-new performance preview for Android phones

Source: Internet
Author: User

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:

 
 
  1. <Html> 
  2.  
  3. <Head> 
  4.  
  5. <Title>PageTitle</Title> 
  6.  
  7. </Head> 
  8.  
  9. <Body> 
  10.  
  11. Thecontentofthebodyelement.
  12.  
  13. </Body> 
  14.  
  15. </Html> 
  16.  
  17. Just like the XML layout of Android, all elements are structured and can be expressed in a tree structure:
  18.  
  19. <?XmlversionXmlversion= "1.0" encoding = "UTF-8 ″?> 
  20.  
  21. <LinearLayout 
  22.  
  23. Http://schemas.android.com/apk/res/android
  24.  
  25. Android: orientation = "vertical"
  26.  
  27. Android: layout_width = "fill_parent"
  28.  
  29. Android: layout_height = "fill_parent"> 
  30.  
  31. <TextView 
  32.  
  33. Android: layout_width = "wrap_content"
  34.  
  35. Android: layout_height = "wrap_content"
  36.  
  37. Android: text = "HelloWorld"/> 
  38.  
  39. </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.

 
 
  1. sensorMan = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);  
  2. sensorMan.registerListener(listener,  
  3.    sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),  
  4.    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.

Related Article

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.