.,,,,,. 11...,,,.,.,.,.
Chapter 14. force feedback.., getsystemservice. String service_name = Context.SENSOR_SERVICE; SensorManager sensormanager = (SensorManager)getSystemService(service_name);. Sensor.,,,. Sensor. Sensor.TYPE_< >.,... Sensor.TYPE_ACCELEROMETER 3. m/s 2..
625 Sensor.TYPE_GYROSCOPE. ( ) Sensor.TYPE_LIGHT. (lux). Sensor.TYPE_MAGNETIC_FIELD ( T). Sensor.TYPE_ORIENTATION. ( ). Sensor.TYPE_PRESSURE. (kpa) Sensor.TYPE_PROXIMITY. (m).,. Sensor.TYPE_TEMPERATURE. ( ),,.., getdefaultsensor.. null. Sensor defaultgyroscope = sensormanager.getdefaultsensor(sensor.type_gyroscope); getsensorlist,..
Chapter 14 List<Sensor> pressuresensors = sensormanager.getsensorlist(sensor.type_pressure); getsensorlist Sensor.TYPE_ALL,. List<Sensor> allsensors = sensormanager.getsensorlist(sensor.type_all);,. 14 1.. SensorEventListener, onsensorchanged, onaccuracychanged. final SensorEventListener mysensoreventlistener = new SensorEventListener() { public void onsensorchanged(sensorevent sensorevent) { // TODO:. ; public void onaccuracychanged(sensor sensor, int accuracy) { // TODO:. onsensorchanged SensorEvent. sensor accuracy. low, medium, high, u reliable.. values ( ) float..
627 timestamp. nanoseconds onaccuracychanged,. accuracy. SensorManager.SENSOR_STATUS_ACCURACY_LOW,. SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM,. SensorManager.SENSOR_STATUS_ACCURACY_HIGH. SensorManager.SENSOR_STATUS_UNRELIABLE..,.,.. Sensor sensor = sensormanager.getdefaultsensor(sensor.type_proximity); sensormanager.registerlistener(mysensoreventlistener, sensor, SensorManager.SENSOR_DELAY_NORMAL);,.. SensorManager.SENSOR_DELAY_FASTEST. SensorManager.SENSOR_DELAY_GAME. SensorManager.SENSOR_DELAY_NORMAL. SensorManager.SENSOR_DELAY_UI UI.
Chapter 14...,.. sensormanager.unregisterlistener(mysensoreventlistener); onresume, onpause. onsensorchanged SensorEvent values. 14 1.,,. http:// developer.android.com/reference/android/hardware/sensorevent.html TYPE_ACCELEROMETER Sensor Manager.GRAVITY_*
629 TYPE_GYROSCOPE TYPE_LIGHT SensorManager.LIGHT_* TYPE_MAGNETIC_FIELD TYPE_ORIENTATION TYPE_PRESSURE TYPE_PROXIMITY TYPE_TEMPERATURE.. Nintendo Wii, AppliPhone, Palm Pre,.,,.,,.
Chapter 14.,. X, Y, Z ( ).,,..,,, augmented reality..,...,, SMS...,. accelerometer. gravitsensor.
631 SensorManager.STANDARD_ GRAVITY,... ( ).,. (X, Lateral), (Y, Longitudinal), (Z, Vertical).. SensorEvent values X Y, Z. 14 1..
Chapter 14.,., X... Y.. Z.,Z 9.8m/s 2.., Sensor.TYPE_ACCELEROMETER SensorEventListener. 12. SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); int sensortype = Sensor.TYPE_ACCELEROMETER; sm.registerlistener(mysensoreventlistener, sm.getdefaultsensor(sensortype), SensorManager.SENSOR_DELAY_NORMAL);, onsensorchanged. onsensorchanged SensorEvent, float X,,.. final SensorEventListener mysensoreventlistener = new SensorEventListener() { public void onsensorchanged(sensorevent sensorevent) { if (sensorevent.sensor.gettype() == Sensor.TYPE_ACCELEROMETER) { float xaxis_laterala = sensorevent.values[0]; float yaxis_longitudinala = sensorevent.values[1]; float zaxis_verticala = sensorevent.values[2];
633 ; // TODO:.,.. 9.8m/s 2. SensorManager.STANDARD_GRAVITY. 1. Forceometer, Forceometer., main.xml,.. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/acceleration" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textstyle="bold" android:textsize="32sp" android:text="center" android:editable="false" android:singleline="true" android:layout_margin="10px"/> /> <TextView android:id="@+id/maxacceleration" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"
Chapter 14 android:textstyle="bold" android:textsize="40sp" android:text="center" android:editable="false" android:singleline="true" android:layout_margin="10px"/> /> </LinearLayout> 2. Forceometer TextView SensorManager. SensorManager sensormanager; TextView accelerationtextview; TextView maxaccelerationtextview; float currentacceleration = 0; float maxacceleration = 0; 3. SensorEventListener... private final SensorEventListener sensoreventlistener = new SensorEventListener() { double calibration = SensorManager.STANDARD_GRAVITY; public void onaccuracychanged(sensor sensor, int accuracy) { public void onsensorchanged(sensorevent event) { double x = event.values[0]; double y = event.values[1]; double z = event.values[2]; import import android.hardware.sensormanager; import android.widget.textview; import import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener;
635 double a = Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2))); currentacceleration = Math.abs((float)(a-calibration)); ; if (currentacceleration > maxacceleration) maxacceleration = currentacceleration; 4. oncreate SensorManager.. @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); accelerationtextview = (TextView)findViewById(R.id.acceleration); maxaccelerationtextview = (TextView)findViewById(R.id.maxAcceleration); sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = sensormanager.getdefaultsensor(sensor.type_accelerometer); sensormanager.registerlistener(sensoreventlistener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); 5.,. updategui. private void updategui() { runonuithread(new Runnable() { public void run() { String currentg = currentacceleration/sensormanager.standard_gravity + "Gs''; accelerationtextview.settext(currentg); import import android.content.context;
Chapter 14 ; ); accelerationtextview.invalidate(); String maxg = maxacceleration/sensormanager.standard_gravity + "Gs''; maxaccelerationtextview.settext(maxg); maxaccelerationtextview.invalidate(); 6., 100ms GUI oncreate. @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); accelerationtextview = (TextView)findViewById(R.id.acceleration); maxaccelerationtextview = (TextView)findViewById(R.id.maxAcceleration); sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = sensormanager.getdefaultsensor(sensor.type_accelerometer); sensormanager.registerlistener(sensoreventlistener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); Timer updatetimer = new Timer("gForceUpdate"); updatetimer.scheduleatfixedrate(new TimerTask() { public void run() { updategui();, 0, 100);. F16,., import import java.util.timer; import java.util.timertask;
637..,. pitch roll..,..,., reference frame.. 14 2 standard reference frame.,.
Chapter 14 ( heading yaw ) X, 0/360 north, 90/360 east, 180/360 south, 270/360 west. Y. 0, ( ) 90, 90, 180/ 180. Z 90 90.0, 90, 90.. 14 3. SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); int sensortype = Sensor.TYPE_ORIENTATION; sm.registerlistener(myorientationlistener, sm.getdefaultsensor(sensortype), SensorManager.SENSOR_DELAY_NORMAL);, SensorEventListener onsensorchanged. SensorEvent values float values (),,. final SensorEventListener myorientationlistener = new SensorEventListener() { public void onsensorchanged(sensorevent sensorevent) { if (sensorevent.sensor.gettype() == Sensor.TYPE_ORIENTATION) { float headingangle = sensorevent.values[0]; float pitchangle = sensorevent.values[1];
639 float rollangle = sensorevent.values[2]; // TODO:. ; public void onaccuracychanged(sensor sensor, int accuracy) {. orientation reference frame X, Y, Z.. onsensorchanged values. 14 4. float[] accelerometervalues; float[] magneticfieldvalues; final SensorEventListener myaccelerometerlistener = new SensorEventListener() { public void onsensorchanged(sensorevent sensorevent) { if (sensorevent.sensor.gettype() == Sensor.TYPE_ACCELEROMETER) accelerometervalues = sensorevent.values; ; public void onaccuracychanged(sensor sensor, int accuracy) { final SensorEventListener mymagneticfieldlistener = new SensorEventListener() { public void onsensorchanged(sensorevent sensorevent) { if (sensorevent.sensor.gettype() == Sensor.TYPE_MAGNETIC_FIELD) magneticfieldvalues = sensorevent.values; ; public void onaccuracychanged(sensor sensor, int accuracy) {
Chapter 14. UI. SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor asensor = sm.getdefaultsensor(sensor.type_accelerometer); Sensor mfsensor = sm.getdefaultsensor(sensor.type_magnetic_field); sm.registerlistener(myaccelerometerlistener, asensor, SensorManager.SENSOR_DELAY_UI); sm.registerlistener(mymagneticfieldlistener, mfsensor, SensorManager.SENSOR_DELAY_UI);, getrotation Matrix getorientation. getorientation degree radian. float[] values = new float[3]; float[] R = new float[9]; SensorManager.getRotationMatrix(R, null, accelerometervalues, magneticfieldvalues); SensorManager.getOrientation(R, values); //. values[0] = (float) Math.toDegrees(values[0]); values[1] = (float) Math.toDegrees(values[1]); values[2] = (float) Math.toDegrees(values[2]); remapcoordinatesystem,.. remapcoordinatesystem.,.
641 remapcoordinatesystem. getrotationmatrixrotation matrix X Y. X Y. (AXIS_X, AXIS_Y, AXIS_Z, AXIS_MINUS_X, AXIS_MINUS_Y, AXIS_MINUS_Z). 14 5 14 3. SensorManager.getRotationMatrix(R, null, avalues, mvalues); float[] outr = new float[9]; SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outr); SensorManager.getOrientation(outR, values); //. values[0] = (float) Math.toDegrees(values[0]); values[1] = (float) Math.toDegrees(values[1]); values[2] = (float) Math.toDegrees(values[2]); 4 CompassView.
Chapter 14,. 1. 4 Compass CompassView Compass. CompassView,. CompassView get/set. float pitch = 0; float roll = 0; public float getpitch() { return pitch; public void setpitch(float pitch) { this.pitch = pitch; public float getroll() { return roll; public void setroll(float roll) { this.roll = roll; 2. ondraw. @Override protected void ondraw(canvas canvas) { [... ondraw... ] 2.1., ( ). int mmeasuredwidth = getmeasuredwidth(); int mmeasuredheight = getmeasuredwidth(); RectF rolloval = new RectF((mMeasuredWidth/3)-mMeasuredWidth/7, (mmeasuredheight/2)-mmeasuredwidth/7,
643 (mmeasuredwidth/3)+mmeasuredwidth/7, (mmeasuredheight/2)+mmeasuredwidth/7 ); markerpaint.setstyle(paint.style.stroke); canvas.drawoval(rolloval, markerpaint); markerpaint.setstyle(paint.style.fill); canvas.save(); canvas.rotate(roll, mmeasuredwidth/3, mmeasuredheight/2); canvas.drawarc(rolloval, 0, 180, false, markerpaint); canvas.restore(); 2.2., forwarangle (). RectF pitchoval = new RectF((2*mMeasuredWidth/3)-mMeasuredWidth/7, (mmeasuredheight/2)-mmeasuredwidth/7, (2*mMeasuredWidth/3)+mMeasuredWidth/7, (mmeasuredheight/2)+mmeasuredwidth/7 ); markerpaint.setstyle(paint.style.stroke); canvas.drawoval(pitchoval, markerpaint); markerpaint.setstyle(paint.style.fill); canvas.drawarc(pitchoval, 0-pitch/2, 180+(pitch), false, markerpaint); markerpaint.setstyle(paint.style.stroke); 3. CompassView., 14 4. 4. Compass.., CompassView SensorManager. import import android.hardware.sensormanager;
Chapter 14 float[] avalues = new float[3]; float[] mvalues = new float[3]; CompassView compassview; SensorManager sensormanager; 5.,, CompassView update Orientation private void updateorientation(float[] values) { if (compassview!= null) { compassview.setbearing(values[0]); compassview.setpitch(values[1]); compassview.setroll(-values[2]); compassview.invalidate(); 6. oncreate CompassView SensorManager,,,. @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); compassview = (CompassView)this.findViewById(R.id.compassView); sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); updateorientation(new float[] {0, 0, 0); 7. calculateorientation private float[] calculateorientation() { float[] values = new float[3]; float[] R = new float[9]; SensorManager.getRotationMatrix(R, null, avalues, mvalues); import import android.content.context;
645 SensorManager.getOrientation(R, values); //. values[0] = (float) Math.toDegrees(values[0]); values[1] = (float) Math.toDegrees(values[1]); values[2] = (float) Math.toDegrees(values[2]); return values; 8. SensorEventListener. onsensorchanged, calculateorientation updateorientation. private final SensorEventListener sensoreventlistener = new SensorEventListener() { public void onsensorchanged(sensorevent event) { if (event.sensor.gettype() == Sensor.TYPE_ACCELEROMETER) avalues = event.values; if (event.sensor.gettype() == Sensor.TYPE_MAGNETIC_FIELD) mvalues = event.values; updateorientation(calculateorientation()); ; public void onaccuracychanged(sensor sensor, int accuracy) { 9. onresume onstop., SensorEventListener. @Override protected void onresume() { super.onresume(); Sensor accelerometer = sensormanager.getdefaultsensor(sensor.type_accelerometer); Sensor magfield = sensormanager.getdefaultsensor(sensor.type_magnetic_field); import import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener;
Chapter 14 sensormanager.registerlistener(sensoreventlistener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); sensormanager.registerlistener(sensoreventlistener, magfield, SensorManager.SENSOR_DELAY_FASTEST); @Override protected void onstop() { sensormanager.unregisterlistener(sensoreventlistener); super.onstop();,. 10.. calculateorientation. private float[] calculateorientation() { float[] values = new float[3]; float[] R = new float[9]; float[] outr = new float[9]; SensorManager.getRotationMatrix(R, null, avalues, mvalues); SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outr); SensorManager.getOrientation(outR, values); //. values[0] = (float) Math.toDegrees(values[0]); values[1] = (float) Math.toDegrees(values[1]); values[2] = (float) Math.toDegrees(values[2]); return values;
647 9 Notifications...,. VIBRATE XML VIBRATE <uses-permission android:name="android.permission.vibrate"/> Vibrator. 14 6 getsystemservice. String vibratorservice = Context.VIBRATOR_SERVICE; Vibrator vibrator = (Vibrator)getSystemService(vibratorService); vibrate. vibrate,.,.. long[] pattern = {1000, 2000, 4000, 8000, 16000 ; vibrator.vibrate(pattern, 0); //. vibrator.vibrate(1000); // 1. cancel..
Chapter 14.,.,,,...., AIDL Wake L cks. TTS,.,.