Android: Alternative to onSensorChanged? no ACCELEROMETER data when phone is lying on its back on a table( when no movement)

点点圈 提交于 2019-12-24 01:01:51

问题


So I'm using this code to get the Accelerometer data. When I am in the DDMS mode to check what my print statement is printing, I noticed that nothing is printed when the phone does not move. ie. its on the table. I think the reason is that onSensorChanged is not called when the phone does not move, and then its called when the phone starts moving again. If i want to capture data regardless of whether the phone is moving or not, should I use some other function? Any Alternatives? or Any solution? Any helps appreciated.

Thanks!

public void onSensorChanged(SensorEvent event) {  

    StringBuffer buff = new StringBuffer();
    buff.append(String.valueOf(event.timestamp));
    buff.append(comma);
    buff.append(String.valueOf(event.values[0]));
    buff.append(comma);
    buff.append(String.valueOf(event.values[1]));
    buff.append(comma);
    buff.append(String.valueOf(event.values[2]));

    mCurrentFile.println(buff.toString());
    mCurrentFile.write(buff.toString()+ '\n');

    System.out.println( "**" + buff.toString());

}

回答1:


You can have one thread listening to the sensor changes (i.e., onSensorChanged() that will write the data into global variables) and another thread running a timer (look at postDelayed, http://developer.android.com/reference/android/os/Handler.html) that will read it at set times. If there are no changes, then it will read the same data again. Easy, clean, and nice to the battery.




回答2:


If this method is only called when "the sensor changes", try saving the values you read when it's executed. If the method is not called that doesn't mean that the values are the same?



来源:https://stackoverflow.com/questions/5804142/android-alternative-to-onsensorchanged-no-accelerometer-data-when-phone-is-lyi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!