Android Learning Week Tenth _ sensors and lbs

Source: Internet
Author: User

1. Basic introduction of the sensor

Through the physical characteristics of the hardware to obtain pressure, gravity, light induction, acceleration and other information. Mainly used in shaking a shake and other mobile phone applications above.

Commonly used sensors on 2.Android phones

In the android2.3 gingerbread system, Google provides a layer for sensor supply use

    • Sensor_type_accelerometer 1//acceleration
    • Sensor_type_magnetic_field 2//Magnetic
    • Sensor_type_orientation 3//direction
    • Sensor_type_gyroscope 4//Gyroscope
    • Sensor_type_light 5//Light Sensing
    • Sensor_type_pressure 6//pressure
    • Sensor_type_temperature 7//temperature
    • Sensor_type_proximity 8//Close
    • Sensor_type_gravity 9//Gravity
    • Sensor_type_linear_acceleration 10//Linear acceleration
    • Sensor_type_rotation_vector 11//Rotation vector
3. How to use
    • Get Sensormanager Object
    • Get Sensor Object
    • Registering a Sensor object
    • Rewrite Onaccuracychanged,onsensorchange these two methods
    • Unregister a Sensor object

Code Description:

    / * Implement Sensoreventlistener, rewrite 2 methods * /     Public  class mainactivity extends appcompatactivity  implements  Sensoreventlistener {    PrivateImageView Msensorimageview;PrivateSensor Msensor;PrivateSensormanager Msensormanager;Private floatMstartdegree =0F@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Msensorimageview = (ImageView) Findviewbyid (R.id.sensor_imageview);/ * Get Sensormanager Object * /Msensormanager = (Sensormanager) getsystemservice (Sensor_service);/ * To get the Sensor object, the parameter determines which type of transducer * /Msensor = Msensormanager.getdefaultsensor (sensor.type_orientation); }/ * Register the Sensor object in the Onresume method * /    @Override    protected void Onresume() {Super. Onresume (); Msensormanager.registerlistener ( This, msensor,sensormanager.sensor_delay_ui); }/ * Unregister the Sensor object in the OnPause method * /    @Override    protected void OnPause() {Super. OnPause (); Msensormanager.unregisterlistener ( This); }@Override     Public void onsensorchanged(Sensorevent event) {if(Event.sensor.getType () = = sensor.type_orientation) {floatdegree =-event.values[0]; Rotateanimation rotateanimation =NewRotateanimation (Mstartdegree,degree,animation.relative_to_self,0.5F,animation.relative_to_self,0.5f); Rotateanimation.setduration ( -);            Msensorimageview.startanimation (rotateanimation);        Mstartdegree = degree; }    }@Override     Public void onaccuracychanged(Sensor sensor,intaccuracy) {}}
4. Precautions

1, not that some phones have all the above sensors
Most handheld devices and desktop devices have acceleration and magnetic sensors, but the design is much less with barometric pressure and thermometers. Also, there can be more than one type of sensor on a single device. For example, a device can have two gravity sensors, each sensor has a different range.
2, different system version introduced the sensor is different

    • For example, many sensors have been introduced since Android1.5 (API level 3), but some of them are not implemented until 2.3 (API 9) is available.
    • Also, some sensors are introduced from Android2.3 and Android4.0. There are two of sensors that are obsolete and replaced by better-updated sensors.
5. Whether the phone has sensor optimization

Method One:

ifnull){            //success        }else{//传感器不存在}

Method Two:
In the Android Market filter to limit the target device must have a specified sensor configuration (if the phone does not have a sensor, you cannot download and install the app)

    • Detecting sensors at run time
if{//success!There‘s a xxx sensor}else{//xxx传感器不存在}
    • Use the Android Market filter to limit the specified sensor configuration that the target device must have
<uses-feature android:name="android:hardware.sensor.accelerometer" android:required="true"/>
6. Precautions for use
    • Don't forget to log out
    • Don't test your app on the simulator
    • Do not block the onsensorchanged () method
    • Avoid using outdated methods or sensor types
    • Verify the sensor before use
    • Carefully select sensor delay
7. Related Open Source project address



8.LBS Foundation

1. What is lbs positioning
LBS is a location-based service application, common to have nearby people, my location, share my location, etc., the takeaway application, according to the current location found nearby restaurants and so on.
2. Main application Scenarios

    • Sign
    • Positioning
    • Navigation
    • Road Map
    • Surrounding Living Services
    • Social
    • People in the vicinity

3, the use of the map
Android native uses Google Maps, so the system provides map services based on Google Maps, but there are a number of restrictions.

4.API Contrast

A. Google Map

    • GPS coordinate system, most stable, satellite map fine
    • But in China by the wall
    • The phone must have a Google Service FrameWork
    • In China basic non-FQ not root is not available

B. Baidu map and map of the German

    • Baidu map, satellite map not fine
    • Support Global positioning, fast function iteration, fast loading of map data
    • High-gold map, good stability. Support Global positioning, but the satellite map is not fine
    • Domestic maps such as the German and Baidu are only Chinese data
    • "Mars coordinate" system

5. Access Baidu Map SDK
A. In the Baidu Map open platform (Baidu Map open platform HTTP://LBSYUN.BAIDU.COM/INDEX.PHP?TITLE=ANDROIDSDK);
B. Application for secret key
C. Follow the instructions to integrate the code

Android Learning Week Tenth _ sensors and lbs

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.