1*d353a188SXin Li 2*d353a188SXin LiAndroid JumpingJack Sample 3*d353a188SXin Li=================================== 4*d353a188SXin Li 5*d353a188SXin LiA basic sample showing how to use the Gravity sensor on the wearable device 6*d353a188SXin Liby counting how many jumping jacks you have performed. 7*d353a188SXin Li 8*d353a188SXin LiIntroduction 9*d353a188SXin Li------------ 10*d353a188SXin Li 11*d353a188SXin Li[SensorEventListener][1] offers you methods used for receiving notifications from the 12*d353a188SXin Li[SensorManager][2] when sensor values have changed. 13*d353a188SXin Li 14*d353a188SXin LiThis example counts how many times Jumping Jacks are performed by detecting the value 15*d353a188SXin Liof the Gravity sensor by the following code: 16*d353a188SXin Li 17*d353a188SXin Li```java 18*d353a188SXin Li@Override 19*d353a188SXin Lipublic void onSensorChanged(SensorEvent event) { 20*d353a188SXin Li detectJump(event.values[0], event.timestamp); 21*d353a188SXin Li} 22*d353a188SXin Li 23*d353a188SXin Liprivate void detectJump(float xValue, long timestamp) { 24*d353a188SXin Li if ((Math.abs(xValue) > GRAVITY_THRESHOLD)) { 25*d353a188SXin Li if(timestamp - mLastTime < TIME_THRESHOLD_NS && mUp != (xValue > 0)) { 26*d353a188SXin Li onJumpDetected(!mUp); 27*d353a188SXin Li } 28*d353a188SXin Li mUp = xValue > 0; 29*d353a188SXin Li mLastTime = timestamp; 30*d353a188SXin Li } 31*d353a188SXin Li} 32*d353a188SXin Li``` 33*d353a188SXin Li 34*d353a188SXin LiThe detectJump method above assumes that when a person is wearing the watch, the x-component of gravity 35*d353a188SXin Lias measured by the Gravity Sensor is +9.8 when the hand is downward and -9.8 when the hand 36*d353a188SXin Liis upward (signs are reversed if the watch is worn on the right hand). Since the upward or 37*d353a188SXin Lidownward may not be completely accurate, we leave some room and instead of 9.8, we use 38*d353a188SXin LiGRAVITY_THRESHOLD (7.0f). We also consider the up <-> down movement successful if it takes less than 39*d353a188SXin LiTIME_THRESHOLD_NS (2000000000 nanoseconds). 40*d353a188SXin Li 41*d353a188SXin Li[1]: http://developer.android.com/reference/android/hardware/SensorEventListener.html 42*d353a188SXin Li[2]: http://developer.android.com/reference/android/hardware/SensorManager.html 43*d353a188SXin Li 44*d353a188SXin LiPre-requisites 45*d353a188SXin Li-------------- 46*d353a188SXin Li 47*d353a188SXin Li- Android SDK 27 48*d353a188SXin Li- Android Build Tools v27.0.2 49*d353a188SXin Li- Android Support Repository 50*d353a188SXin Li 51*d353a188SXin LiScreenshots 52*d353a188SXin Li------------- 53*d353a188SXin Li 54*d353a188SXin Li<img src="screenshots/jumping_jack.gif" height="400" alt="Screenshot"/> 55*d353a188SXin Li 56*d353a188SXin LiGetting Started 57*d353a188SXin Li--------------- 58*d353a188SXin Li 59*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the 60*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio. 61*d353a188SXin Li 62*d353a188SXin LiSupport 63*d353a188SXin Li------- 64*d353a188SXin Li 65*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968 66*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android 67*d353a188SXin Li 68*d353a188SXin LiIf you've found an error in this sample, please file an issue: 69*d353a188SXin Lihttps://github.com/googlesamples/android-JumpingJack 70*d353a188SXin Li 71*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and 72*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 73*d353a188SXin Li 74*d353a188SXin LiLicense 75*d353a188SXin Li------- 76*d353a188SXin Li 77*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc. 78*d353a188SXin Li 79*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor 80*d353a188SXin Lilicense agreements. See the NOTICE file distributed with this work for 81*d353a188SXin Liadditional information regarding copyright ownership. The ASF licenses this 82*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not 83*d353a188SXin Liuse this file except in compliance with the License. You may obtain a copy of 84*d353a188SXin Lithe License at 85*d353a188SXin Li 86*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0 87*d353a188SXin Li 88*d353a188SXin LiUnless required by applicable law or agreed to in writing, software 89*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT 90*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 91*d353a188SXin LiLicense for the specific language governing permissions and limitations under 92*d353a188SXin Lithe License. 93