当陀螺仪读数x = 0.0,y = 0.0,z = 0.0&时,我希望将时间存储为“dateString”.当x> 0.0&& y> 0.0&& z> 0.0将时间存储为“dateString1”.
如果条件得到满足,我无法计时,因为它占用了我手机的当前时间.它一直在运行,因此无法在事件发生时获得时间解决方案是什么?
示例 – 如果我的手机处于静止状态,陀螺仪传感器将提供x = 0.0,y = 0.0,z = 0.0如果读数在三小时内仍然相同.假设这是手机睡眠的开始时间.当读数会在x,y,z方面突然改变时,它将是唤醒时间.
如何检测这种情况(睡眠的开始时间和唤醒时间)?
下面的代码是检测陀螺仪读数x,y,z,它仍然分别显示0.0,0.0,0.0.
当pic,x,y,z的电话值增加时,我们如何添加代码来检测时间,如上所述.
import androID.app.Activity; import androID.content.Context; import androID.harDWare.Sensor; import androID.harDWare.SensorEvent; import androID.harDWare.SensorEventListener; import androID.harDWare.SensorManager; import androID.os.Bundle; import androID.Widget.TextVIEw; import java.text.SimpleDateFormat; import java.util.Calendar; public class Main extends Activity { Dbhelper myDb; //variable for x,y,z TextVIEw textx, textY, textZ;button buttonVIEw,buttonAdd; TextVIEw text1,text2,text3,text4 ; SensorManager sensorManager; Sensor sensor; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); myDb =new Dbhelper(this); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensor = sensorManager.getDefaultSensor(Sensor.TYPE_gyroscope); textx = (TextVIEw) findVIEwByID(R.ID.textx); textY = (TextVIEw) findVIEwByID(R.ID.textY); textZ = (TextVIEw) findVIEwByID(R.ID.textZ); text1 = (TextVIEw) findVIEwByID(R.ID.text1); text2 = (TextVIEw) findVIEwByID(R.ID.text2); text3 = (TextVIEw) findVIEwByID(R.ID.text3); text4 = (TextVIEw) findVIEwByID(R.ID.text4); buttonVIEw=(button)findVIEwByID(R.ID.button_vIEw); buttonAdd=(button)findVIEwByID(R.ID.button_add); AddData(); vIEwAll(); } public voID AddData() { buttonAdd.setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { boolean isInserted = myDb.insertData(text1.getText().toString(), textx.getText().toString(), textY.getText().toString(), textZ.getText().toString()); if (isInserted == true) Toast.makeText(Main.this, "Data Inserted", Toast.LENGTH_LONG).show(); else Toast.makeText(Main.this, "Data not Inserted", Toast.LENGTH_LONG).show(); } } );} public voID vIEwAll() { buttonVIEw.setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Cursor res = myDb.getAllData(); if(res.getCount() == 0) { // show message showMessage("Error","nothing found"); return; } StringBuffer buffer = new StringBuffer(); while (res.movetoNext()) { buffer.append("ID :"+ res.getString(0)+"\n"); buffer.append("TIME :"+res.getString(1)+"\n"); buffer.append("X :"+ res.getString(2)+"\n"); buffer.append("Y :"+ res.getString(3)+"\n"); buffer.append("Z :"+ res.getString(4)+"\n\n"); } // Show all data showMessage("Data",buffer.toString()); } } ); } public voID showMessage(String Title,String Message){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(true); builder.setTitle(Title); builder.setMessage(Message); builder.show(); } public voID onResume() { super.onResume(); sensorManager.registerListener(gyroListener, sensor,SensorManager.SENSOR_DELAY_norMAL); } public voID onStop() { super.onStop(); sensorManager.unregisterListener(gyroListener); } public SensorEventListener gyroListener = new SensorEventListener() { public voID onAccuracyChanged(Sensor sensor, int acc) { } public voID onSensorChanged(SensorEvent event) { long timeStamp = event.timestamp; double x = event.values[0]; x=(double)Math.round(x * 10d) / 10d; //x=Math.round(2); double y = event.values[1]; y=(double)Math.round(y * 10d) / 10d; double z = event.values[2]; z=(double)Math.round(z * 10d) / 10d; textx.setText("X : " + x ); textY.setText("Y : " + y ); textZ.setText("Z : " + z ); Calendar c2 = Calendar.getInstance(); SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss"); String dateString1 =sdf1.format(c2.getTime()); int timeOfDay = c2.get(Calendar.HOUR_OF_DAY); c2.getTime(); Calendar c1 = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String dateString =sdf.format(c1.getTime()); int timeOfDay1 = c1.get(Calendar.HOUR_OF_DAY); c1.getTime(); if(timeOfDay >= 11 && timeOfDay <= 20 && x==0 && y==0 && z==0) text1.setText("phone is not moving" + dateString); else if (timeOfDay1 >= 11 && timeOfDay1 <= 20 && x>0 || y>0 || z>0) text2.setText("phone is just moved " + dateString1); else if(timeOfDay >= 11 && timeOfDay <= 20 ) text3.setText("Were phone asleep between "+ dateString + "&" + dateString1); } };}
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="match_parent" androID:layout_height="match_parent" androID:gravity="center" > <TextVIEw androID:ID="@+ID/textx" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="" /> <TextVIEw androID:ID="@+ID/textY" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="20sp" androID:textSize="30sp" androID:text="" /> <TextVIEw androID:ID="@+ID/textZ" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="20sp" androID:textSize="30sp" androID:text="" /> <TextVIEw androID:ID="@+ID/text1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="20sp" androID:textSize="30sp" androID:text="" /> <TextVIEw androID:ID="@+ID/text2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="20sp" androID:textSize="30sp" androID:text="" /> <TextVIEw androID:ID="@+ID/text3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="20sp" androID:textSize="30sp" androID:text="" /></linearLayout>
注意:请不要告诉Timestamp是我已经尝试过的解决方案.
解决方法:
在SensorEventListener中,您可以使用回调参数SensorEvent事件来使用以下代码获取时间戳,
long timeStamp = event.timestamp;
注意:event.timestamp将返回long.
编辑:根据您的评论更新答案.
要检查设备是否移动超过三个小时,请使用以下代码.
初始化long lastTimeStamp = 0;在您班级的全局变量中.
SensorEventListener sensorEventListener = new SensorEventListener() { @OverrIDe public voID onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; if(Math.round(x) == 0 && Math.round(y) == 0 && Math.round(z) == 0) { //device dIDn't move - store current timestamp lastTimeStamp = event.timestamp; } else { //below if condition is to check if device hasn't been IDle if (lastTimeStamp == 0) { return; } //(3 * 60 * 60 * 1000) checks if device wasn't moved for more than 3 hours if ((event.timestamp - lastTimeStamp) > (3 * 60 * 60 * 1000)) { //code goes here } lastTimeStamp = 0; } } @OverrIDe public voID onAccuracyChanged(Sensor sensor, int accuracy) { } };
希望这会更清晰.
总结以上是内存溢出为你收集整理的android – 如果某些条件遇到陀螺仪阅读,如何获得时间全部内容,希望文章能够帮你解决android – 如果某些条件遇到陀螺仪阅读,如何获得时间所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)