资源预览内容
第1页 / 共15页
第2页 / 共15页
第3页 / 共15页
第4页 / 共15页
第5页 / 共15页
第6页 / 共15页
第7页 / 共15页
第8页 / 共15页
第9页 / 共15页
第10页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 register 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) 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 reference 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 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 determine 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_;public 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 location 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 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(LocationManager.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 in 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)Overridepublic 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 longitude = 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.distanceTo(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 resume 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);protected 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 from 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,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号