1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Copyright 2013 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16--> 17 18 19 20<sample> 21 <name>JumpingJack</name> 22 <group>Wearable</group> 23 <package>com.example.android.wearable.jumpingjack</package> 24 25 <targetSdkVersionWear>25</targetSdkVersionWear> 26 27 <strings> 28 <intro> 29 <![CDATA[ 30 Uses the Gravity sensor to count how many jumping jacks you have performed. 31 ]]> 32 </intro> 33 </strings> 34 35 <template src="base-build"/> 36 <template src="Wear"/> 37 <common src="logger"/> 38 <common src="activities"/> 39 40 <metadata> 41 <status>PUBLISHED</status> 42 <categories>Wearable</categories> 43 <technologies>Android</technologies> 44 <languages>Java</languages> 45 <solutions>Mobile</solutions> 46 <level>INTERMEDIATE</level> 47 <icon>screenshots/web-icon.png</icon> 48 <screenshots> 49 <img>screenshots/jumping_jack.gif</img> 50 </screenshots> 51 <api_refs> 52 <android>android.hardware.SensorEvent</android> 53 <android>android.hardware.SensorEventManager</android> 54 </api_refs> 55 56 <description> 57<![CDATA[ 58A basic sample showing how to use the Gravity sensor on the wearable device 59by counting how many jumping jacks you have performed. 60]]> 61 </description> 62 63 <intro> 64<![CDATA[ 65[SensorEventListener][1] offers you methods used for receiving notifications from the 66[SensorManager][2] when sensor values have changed. 67 68This example counts how many times Jumping Jacks are performed by detecting the value 69of the Gravity sensor by the following code: 70 71```java 72@Override 73public void onSensorChanged(SensorEvent event) { 74 detectJump(event.values[0], event.timestamp); 75} 76 77private void detectJump(float xValue, long timestamp) { 78 if ((Math.abs(xValue) > GRAVITY_THRESHOLD)) { 79 if(timestamp - mLastTime < TIME_THRESHOLD_NS && mUp != (xValue > 0)) { 80 onJumpDetected(!mUp); 81 } 82 mUp = xValue > 0; 83 mLastTime = timestamp; 84 } 85} 86``` 87 88The detectJump method above assumes that when a person is wearing the watch, the x-component of gravity 89as measured by the Gravity Sensor is +9.8 when the hand is downward and -9.8 when the hand 90is upward (signs are reversed if the watch is worn on the right hand). Since the upward or 91downward may not be completely accurate, we leave some room and instead of 9.8, we use 92GRAVITY_THRESHOLD (7.0f). We also consider the up <-> down movement successful if it takes less than 93TIME_THRESHOLD_NS (2000000000 nanoseconds). 94 95[1]: http://developer.android.com/reference/android/hardware/SensorEventListener.html 96[2]: http://developer.android.com/reference/android/hardware/SensorManager.html 97]]> 98 </intro> 99 </metadata> 100</sample> 101