accelerometer - Android detect phone lifting action -


i want perform activity when user lifts phone flat surface. method using right detect shake motion using phone's accelerometer using following code:

sensorman = (sensormanager) getsystemservice(sensor_service);         accelerometer = sensorman.getdefaultsensor(sensor.type_accelerometer);  sensorman.registerlistener(this, accelerometer, sensormanager.sensor_status_accuracy_high);  public void onsensorchanged(sensorevent event) {         if (event.sensor.gettype() == sensor.type_accelerometer) {             mgravity = event.values.clone();             // shake detection             float x = mgravity[0];             float y = mgravity[1];             float z = mgravity[2];              maccellast = maccelcurrent;             maccelcurrent = floatmath.sqrt(x * x + y * y + z * z);             float delta = maccelcurrent - maccellast;             maccel = maccel * 0.9f + delta;              if (maccel > 0.9) {                 //perform tasks.             }         } 

the issue facing code 0.9f threshold reached if phone still on flat surface. tried logging maccel value , found rannging 9.0 0.4 when phone not touched. there guaranteed way detect phone's lift movement?

solved issue. wanted check "y" value stated in question , check if value greater 1.0.

note that, if phone kept in vertical position y around 9.8 in such cases can check x instead. in case user had lift phone , somewhen tilt phone put check if(y >= 1.0 && y <= 2.0);

edit : updated code

    @override public void onsensorchanged(sensorevent event) {     try {         if (event.sensor.gettype() == sensor.type_accelerometer) {             mgravity = event.values.clone();             // shake detection             float x = mgravity[0];             float y = mgravity[1];             float z = mgravity[2];              float yabs = math.abs(mgravity[1]);              maccellast = maccelcurrent;             maccelcurrent = floatmath.sqrt(x * x + y * y + z * z);             float delta = maccelcurrent - maccellast;             maccel = maccel * 0.9f + delta;              if (yabs > 2.0 && yabs < 4.0 && !isalerted() && !iscallactive()) {                 alert();             }         }     } catch (exception e) {         e.printstacktrace();     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -