【发布时间】:2017-05-12 10:06:08
【问题描述】:
当陀螺仪读取 x=0.0,y=0.0,z=0.0 & 当 x>0.0 && y>0.0 && z>0.0 将时间存储为“dateString1”时,我想将时间存储为“dateString”。 如果满足条件,我无法计时,因为它正在占用我手机的当前时间。它一直在运行,因此无法在事件发生时获得时间,相同的解决方案是什么?
例如 - 如果我的手机处于休息状态,陀螺仪传感器将提供 x=0.0,y=0.0,z=0.0 如果读数在三个小时内仍然相同。假设这是手机睡眠的开始时间。当读数在 x,y,z 方面突然发生变化时,它将是唤醒时间。
如何检测这种情况(睡眠开始时间和起床时间)?
下面的代码检测陀螺仪读数 x ,y ,z 当它静止时分别显示 0.0,0.0,0.0。
当获取x,y,z的phone值时只增加那么我们如何添加代码来检测上面的场景。
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 是我已经尝试过的解决方案。
【问题讨论】:
-
你在哪里检查 x y z 是否不是 0
-
else if (timeOfDay1 >= 11 && timeOfDay1 0 || y>0 || z>0) text2.setText("手机刚刚移动" + dateString1);
标签: android accelerometer android-sensors gyroscope motion-detection