35
DONG NAI UNIVERSITY OF TECHNOLOGY 1 1. Sensors 2. Monitoring the Battery

7 - Thao Tác Với Thiết Bị Cảm Ứng

  • Upload
    cobo

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 7 - Thao Tác Với Thiết Bị Cảm Ứng

1

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

2. Monitoring the Battery

Page 2: 7 - Thao Tác Với Thiết Bị Cảm Ứng

2

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

The emulator does not provide any sensor data. All sensor testing must be done on a physical device. Alternatively, OpenIntents.org also provides a handy Sensor Simulator: http://code.google.com/p/openintents/wiki/SensorSimulator This tool simulates accelerometer, compass, and temperature sensors, and it transmits data to the emulator.

Android devices have a variety of sensors :

Page 3: 7 - Thao Tác Với Thiết Bị Cảm Ứng

3

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

TYPE_ACCELEROMETER: Measures acceleration in three directions; values are in SI units (m/s2).TYPE_GYROSCOPE: Measures angular orientation in three directions; values are angles in degrees.TYPE_LIGHT: Measures ambient light; values are in SI lux units.TYPE_MAGNETIC_FIELD: Measures magnetism in three directions; the compass values are in micro-Tesla (uT).TYPE_PRESSURE: Measures barometric pressure.TYPE_PROXIMITY: Measures the distance to an object; values are in centimeters, or “near” versus “far.”TYPE_RELATIVE_HUMIDITY: Measures the relative humidity.TYPE_AMBIENT_TEMPERATURE: Measures temperature.

Page 4: 7 - Thao Tác Với Thiết Bị Cảm Ứng

4

DONG NAI UNIVERSITY OF TECHNOLOGY

1. SensorsHere are the major classes related to sensor.

Class Comment

Sensor Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.

SensorEvent This class represents a Sensor event and holds informations such as the sensor's type, the time-stamp, accuracy and of course the sensor's data.

SensorEventListener An interface: Used for receiving notifications from the SensorManager when sensor values have changed.

SensorManager SensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.

Page 5: 7 - Thao Tác Với Thiết Bị Cảm Ứng

5

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Working with Sensor

To get a Sensor, you need to use SensorManager.

Page 6: 7 - Thao Tác Với Thiết Bị Cảm Ứng

6

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Working with Sensor

Register EventListener to it.

Page 7: 7 - Thao Tác Với Thiết Bị Cảm Ứng

7

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Working with Sensor

UnRegister EventListener.

Register in the onResume method and Unregister in the onPause method

Page 8: 7 - Thao Tác Với Thiết Bị Cảm Ứng

8

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

SensorManager.SENSOR_DELAY

Frequence Comment

SENSOR_DELAY_FASTEST get sensor data as fast as possible

SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes

SENSOR_DELAY_GAME rate suitable for gamesSENSOR_DELAY_UI rate suitable for the user

interface

Page 9: 7 - Thao Tác Với Thiết Bị Cảm Ứng

9

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

Receive SensorEvent Normally, onSensorChanged is the method in which we need to put your sensor handle logic.

Page 10: 7 - Thao Tác Với Thiết Bị Cảm Ứng

10

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors SensorEvent

Type Name Commentpublic int accuracy The accuracy of this event.

public Sensor sensor The sensor that generated this event.

public long timestamp The time in nanosecond at which the event happened

public final float[] values The length and contents of the values array depends on which sensor type is being monitored.

Page 11: 7 - Thao Tác Với Thiết Bị Cảm Ứng

11

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_TEMPERATURE

Page 12: 7 - Thao Tác Với Thiết Bị Cảm Ứng

12

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_TEMPERATURE

Page 13: 7 - Thao Tác Với Thiết Bị Cảm Ứng

13

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_LIGHT

Page 14: 7 - Thao Tác Với Thiết Bị Cảm Ứng

14

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_LIGHT

Page 15: 7 - Thao Tác Với Thiết Bị Cảm Ứng

15

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_PRESSURE

Page 16: 7 - Thao Tác Với Thiết Bị Cảm Ứng

16

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_PRESSURE

Page 17: 7 - Thao Tác Với Thiết Bị Cảm Ứng

17

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor. TYPE_PROXIMITY

Page 18: 7 - Thao Tác Với Thiết Bị Cảm Ứng

18

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor. TYPE_PROXIMITY

Page 19: 7 - Thao Tác Với Thiết Bị Cảm Ứng

19

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor. TYPE_PROXIMITY

Page 20: 7 - Thao Tác Với Thiết Bị Cảm Ứng

20

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ACCELEROMETER

The accelerometer values are represented in SI units (m/s2). The device at right has the bottom of the device screen pointing toward the center of gravity. Gravity on Earth is 9.80665 m/s2.

All values are in SI units (m/s2)values[0]: Acceleration minus Gx on the x-axisvalues[1]: Acceleration minus Gy on the y-axisvalues[2]: Acceleration minus Gz on the z-axis

Page 21: 7 - Thao Tác Với Thiết Bị Cảm Ứng

21

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ACCELEROMETER

Use to Accelerometer to detect SHAKING Device

Page 22: 7 - Thao Tác Với Thiết Bị Cảm Ứng

22

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ACCELEROMETER

Page 23: 7 - Thao Tác Với Thiết Bị Cảm Ứng

23

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ACCELEROMETER

Page 24: 7 - Thao Tác Với Thiết Bị Cảm Ứng

24

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_GYROSCOPE

measure, or maintain, the orientation of a device. rate of rotation around a particular axis

values[0]: Angular speed around the x-axisvalues[1]: Angular speed around the y-axisvalues[2]: Angular speed around the z-axis

Page 25: 7 - Thao Tác Với Thiết Bị Cảm Ứng

25

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_GYROSCOPE

Page 26: 7 - Thao Tác Với Thiết Bị Cảm Ứng

26

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_GYROSCOPE

Page 27: 7 - Thao Tác Với Thiết Bị Cảm Ứng

27

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ORIENTATION

All values are angles in degrees:values[0]: Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West

values[1]: Pitch, rotation around x-axis (-180 to 180), with positive values when the z-axis moves toward the y-axis.

values[2]: Roll, rotation around y-axis (-90 to 90), with positive values when the x-axis moves toward the z-axis.

Page 28: 7 - Thao Tác Với Thiết Bị Cảm Ứng

28

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors Sensor.TYPE_ORIENTATION

Page 29: 7 - Thao Tác Với Thiết Bị Cảm Ứng

29

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Sensors

Exercise : Using Sensors Accelerometor and Proximity to build an AlbumManager application:- Nice GUI to display Pictures- When Device was shaken to Left open the Previous Picture- When Device was shaken to Right open the Next Picture- When Proximity is active and event.values[0]==0.0 Close the

AlbumManager application

Page 30: 7 - Thao Tác Với Thiết Bị Cảm Ứng

30

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Monitoring the Battery

A monitoring application can reduce the monitoring frequency when the battery is low and can increase it if the device is powered by an external power source. The battery levels can also help indicate the efficiency of an application, allowing developers to find areas where behavior can be modified to improve battery life, which would be appreciated by users.

To monitor the battery, the application must have the BATTERY_STATS permission:

<uses-permission android:name="android.permission.BATTERY_STATS"/>

Page 31: 7 - Thao Tác Với Thiết Bị Cảm Ứng

31

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Monitoring the Battery

Then the application needs to register for a particular BroadcastIntent. In this case, it must be Intent.ACTION_BATTERY_CHANGED.

Emulator Physical Device

Page 32: 7 - Thao Tác Với Thiết Bị Cảm Ứng

32

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Monitoring the Battery

Page 33: 7 - Thao Tác Với Thiết Bị Cảm Ứng

33

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Monitoring the Battery

Page 34: 7 - Thao Tác Với Thiết Bị Cảm Ứng

34

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Monitoring the Battery

Page 35: 7 - Thao Tác Với Thiết Bị Cảm Ứng

35

DONG NAI UNIVERSITY OF TECHNOLOGY

END