AndroidSensors&AsyncCallbacks

上传人:lizhe****0001 文档编号:55161715 上传时间:2018-09-25 格式:PPT 页数:15 大小:720KB
返回 下载 相关 举报
AndroidSensors&AsyncCallbacks_第1页
第1页 / 共15页
AndroidSensors&AsyncCallbacks_第2页
第2页 / 共15页
AndroidSensors&AsyncCallbacks_第3页
第3页 / 共15页
AndroidSensors&AsyncCallbacks_第4页
第4页 / 共15页
AndroidSensors&AsyncCallbacks_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《AndroidSensors&AsyncCallbacks》由会员分享,可在线阅读,更多相关《AndroidSensors&AsyncCallbacks(15页珍藏版)》请在金锄头文库上搜索。

1、Android Sensors & Async Callbacks,Android Sensors Overview,Android Sensors:MIC Camera Temperature Location (GPS or Network) Orientation Accelerometer Proximity Pressure Light,Async Callbacks,Androids sensors are controlled by external services and only send events when they choose toAn app must regi

2、ster a callback to be notified of a sensor eventEach sensor has a related XXXListener interface that your callback must implement E.g. LocationListener,SensorManager,Your App,Sensor Event,Sensor Event,Sensor Event,Register Callback,Getting the Relevant System Service,The non-media (e.g. not camera)

3、sensors are managed by a variety of XXXXManager classes: LocationManager (GPS) SensorManager (accelerometer, gyro, proximity, light, temp) The first step in registering is to obtain a reference to the relevant manager Every Activity has a getSystemService() method that can be used to obtain a refere

4、nce to the needed manager,public class MyActivity private SensorManager sensorManager_;public void onCreate()sensorManager_ = (SensorManager) getSystemService(SENSOR_SERVICE);,Registering for Location Updates,The LocationManager handles registrations for GPS and network location updates In order for

5、 an object to receive updates from GPS, it must implement the LocationListener interface Once the LocationManager is obtained, an object registers for updates by calling requestLocationUpdates (there are multiple versions you can use) The arguments passed into the requestLocationUpdates method deter

6、mine the granularity of location changes that will generate an event send updates that are at least X meters apart send updates at least this far apart in time send updates that have this minimum accuracy,public class MyActivity implements LocationListenerprivate LocationManager locationManager_;pub

7、lic void onCreate()locationManager_ = (LocationManager) getSystemService(LOCATION_SERVICE);locationManager_.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, Criteria.ACCURACY_FINE, this);,Location Providers,The phones location can be determined from multiple providers GPS Network GPS locatio

8、n updates consume significantly more power than network location updates but are more accurate GPS: 25 seconds * 140mA = 1mAh Network: 2 seconds * 180mA = 0.1mAh The provider argument determines which method will be used to get a location for you You can also register for the PASSIVE_PROVIDER which

9、only updates you if another app is actively using GPS / Network location,public class MyActivity implements LocationListenerprivate LocationManager locationManager_;public void onCreate()locationManager_ = (LocationManager) getSystemService(LOCATION_SERVICE);locationManager_.requestLocationUpdates(L

10、ocationManager.PASSIVE_PROVIDER, 10, Criteria.ACCURACY_FINE, this);,The LocationListener Interface,public class MyActivity implements LocationListener/ Called when your GPS location changesOverridepublic void onLocationChanged(Location location) / Called when a provider gets turned off by the user i

11、n the settingsOverridepublic void onProviderDisabled(String provider) / Called when a provider is turned on by the user in the settingsOverridepublic void onProviderEnabled(String provider) / Signals a state change in the GPS (e.g. you head through a tunnel and/ it loses its fix on your position)Ove

12、rridepublic void onStatusChanged(String provider, int status, Bundle extras) ,Location Information,public class MyActivity implements LocationListener/ Called when your GPS location changesOverridepublic void onLocationChanged(Location location) double altitude = location.getAltitude();double longit

13、ude = location.getLongitude();double latitude = location.getLatitude();float speed = location.getSpeed(); float bearing = location.getBearing();float accuracy = location.getAccuracy(); /in meterslong time = location.getTime(); /when the fix was obtained/ Other useful Location functions:/ location.di

14、stanceTo(dest)/ location.bearingTo(dest),Being a Good Citizen,It is very important that you unregister your App when you no longer need updates For example, you should always unregister your listener when your Activity is paused! If you unregister when you pause, you must also reregister when you re

15、sume This is true for all sensors!,public class MyActivity private LocationManager locationManager_;public void onCreate(Bundle savedInstanceState) locationManager_ = (LocationManager)getSystemService(LOCATION_SERVICE); protected void onPause() super.onPause();locationManager_.removeUpdates(this);pr

16、otected void onResume() super.onResume();locationManager_.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, Criteria.ACCURACY_FINE, this); ,Registering for Sensor Updates,The SensorManager handles registrations for Accelerometer, Temp, Light, Gyro In order for an object to receive updates fro

17、m GPS, it must implement the SensorEventListener interface Once the SensorManager is obtained, you must obtain a reference to the specific sensor you are interested in updates from The arguments passed into the registerListener method determine the sensor that you are connected to and the rate at which it will send you updates,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号