- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当陀螺仪读数 x=0.0,y=0.0,z=0.0 & 当 x>0.0 && y>0.0 && z>0.0 时,我想将时间存储为“dateString”,将时间存储为“dateString1”。如果满足条件,我无法计时,因为它占用了我手机的当前时间。它一直在运行,所以无法在事件发生时获得时间,解决方案是什么?
示例 - 如果我的手机处于静止状态,陀螺仪传感器将提供 x=0.0,y=0.0,z=0.0 如果读数在三个小时内仍然相同。假设这是手机 sleep 的开始时间。当阅读在 x、y、z 方面突然发生变化时,就是起床时间。
如何检测这种情况( sleep 开始时间和唤醒时间)?
下面的代码正在检测陀螺仪读数 x , y , z 当它静止时它分别显示 0.0,0.0,0.0 。
当 pic up 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);
}
};
}
主.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 - 如果满足陀螺仪读数等条件,如何获得时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356698/
我想检索具有多个条件的数据,其中每个条件将在特定字段中包含特定关键字。 表结构如下: sid nid cid 数据 50 7 5 ee 50 7 6 AA 50 7 8 ff 51 7 5 ee 51
在 Prolog 中,我经常通过提供模板(包含变量的结构)然后满足其上的一组约束来解决问题。一个简单的例子可能是: go(T) :- T = [_, _, _], member(cat
在设计 FPGA 系统时,我如何粗略估计给定任务所需的逻辑 block 数量? 有人对我对这些常见设备的期望有一个粗略的数量级吗? 串口 使用 CRC32 的数据包解帧器 8 微核 我看过 www.o
我需要编写一段代码,如果函数满足列表中的大多数元素,则返回 True,不满足其中的 false。例如:moreThan odd [1,2,3] 是 True,但是 moreThan odd [1,2,
一旦满足三个条件,我需要使用 componentWillReceiveProps() 来调用我的组件中的方法。其中两个条件将当前 Prop 与下一个 Prop 进行比较,这两个条件通过 Ajax 请求
我正在构建一个主从表单。主视图模型构造细节 View 模型的实例。这些细节 View 模型有几个依赖项,需要用新 类实例来满足。 (这是因为他们需要在独立于主虚拟机的数据上下文中运行的服务层。) 实现
我有以下项目,我已经使用了一段时间。正如您在运行 snnipets 后看到的那样,一切正常。 /* The dark background behind the dialogs */ .dialog-
我正在尝试找出解决此问题的方法: 我想要一个函数来检查文本字段是否填充了文本并且复选框是否被选中。当满足这些条件时,“提交”按钮将启用。如果启用“提交”按钮后不久,用户清除文本字段或取消选中复选框,则
所以我相对较新,我有以下代码,我想知道如何制作这样我可以返回临时变量,同时满足java的返回要求。我希望返回临时值,但由于它位于 if-else block 内,因此从技术上讲,它不会在其外部初始化。
我正在编写一个脚本,该脚本读取文本文件并根据 .txt 文件的内容更改 div 中的文本。 但这不是我的问题。我不想要纯文本,背景颜色应该根据满足 if/elseif/else 函数的条件而改变。 v
我想在 if let 构造中满足多个约束。我知道我们可以使用“,”(逗号)来解包多个值,但它们都必须成功解包。 例如: var str: String? = "Hello" var x: Int? =
当我在 genymotion 模拟设备上安装我的应用程序时,它无法很好地安装,在控制台上我得到“INSTALL_FAILED_CPU_ABI_INCOMPATIBLE”我尝试了另一个应用程序,它安装得
因此,我试图根据数据帧的匹配条件来查看数据帧的两个变量(v1 和 v2)是否在其符号(正数或负数)中匹配变量(ID1==ID2)。 示例数据框 - Trial.df: ID1 v1
如果交付一个 Java 应用程序,它使用 gradle 依赖管理和许多来自 maven-central 的开源库,是否足以检查第一级 depedencies 的许可证(因为他们的依赖关系必须再次自动与
我正在尝试创建一个满足接口(interface) Iterable 的类“Gprogram” (这样我就可以在我的 Gprogram 中迭代 Gcommand)。但是,我只能使用类型 Iterable
我想知道是否可以获得一些帮助。 我试图在查询中写入一个查询,我使用 3 个字段:ID、选项和金额。 我需要对我的唯一 ID 进行分组,然后在该组中我需要按选项白色进行拆分,总计每个选项的金额。例如:编
如何在iOS swift项目中配置Jitsi-meet框架开启视频通话服务? 最佳答案 编辑:这也适用于 Xcode Version 12.2 (12B45b)在 Mac OS Big Sur 上。
我正在玩一些交互式菜单,目前有一个隐藏菜单,当按下一个按钮时,它会从右边出现,并将整个内容移到上面。有点像移动 facebook 应用程序。为了确定按钮应该将菜单滑出还是放回我使用 javascrip
我的目标很简单,使用遗传算法重现经典的“Hello, World”字符串。 我的代码基于此 post .代码主要包含4个部分: 生成具有多个不同个体的种群 根据与target的比较,定义评估个体好坏的
问题陈述 我们有一个雇主想要面试 N 个人,因此安排了 N 个面试时段。每个人都有这些时段的忙闲时间表。给出一个算法,如果可能的话将 N 个人安排到 N 个槽位,如果不可能则返回一个标志/错误/等。最
我是一名优秀的程序员,十分优秀!