Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何计算长时间内的时间_Android_Time_Timer_Count_Accelerometer - Fatal编程技术网

Android-如何计算长时间内的时间

Android-如何计算长时间内的时间,android,time,timer,count,accelerometer,Android,Time,Timer,Count,Accelerometer,在我的应用程序中,我想测量加速度计静止的时间 例如,如果我将阈值设置为0.4 m/s,并且加速计速度始终低于此值,则表示手机处于静止状态(阈值用于说明值中的抖动) 我希望能够计算手机处于静止状态的时间,以及达到一定的静止时间后,然后对该事件做出反应 我的加速计已经可以正常工作了,这次我只需要数一数,我不知道怎么做,有人能帮我吗?提前谢谢 解决方案: start = System.currentTimeMillis(); if (vectorTotal > 10.1 || ve

在我的应用程序中,我想测量加速度计静止的时间

例如,如果我将阈值设置为0.4 m/s,并且加速计速度始终低于此值,则表示手机处于静止状态(阈值用于说明值中的抖动)

我希望能够计算手机处于静止状态的时间,以及达到一定的静止时间后,然后对该事件做出反应

我的加速计已经可以正常工作了,这次我只需要数一数,我不知道怎么做,有人能帮我吗?提前谢谢

解决方案:

  start = System.currentTimeMillis();

    if (vectorTotal > 10.1 || vectorTotal < 9.5){
        timelimit = (System.currentTimeMillis()+ 10000);
    }
    else if (start >= timelimit){
        checkUser();
        timelimit = (System.currentTimeMillis()+ 10000);
    }
start=System.currentTimeMillis();
如果(矢量总数>10.1 | |矢量总数<9.5){
timelimit=(System.currentTimeMillis()+10000);
}
否则如果(开始>=时间限制){
checkUser();
timelimit=(System.currentTimeMillis()+10000);
}

你的应用程序在前台吗

开始时:

long start = System.currentTimeMillis();
如有必要,将此值保存在数据库或首选项中

最后:

long end = System.currentTimeMillis();
然后:


我认为您可能正在寻找一种双重服务/活动解决方案,其中服务在后台运行,计算您想要计算的任何数量,并在活动要求时向其提供此信息


就计数时间而言,这仅仅是操纵DKIT Android指出的从
System.currentTimeMillis()返回的返回。

你可以在每次超过阈值时设置警报,当警报最终触发时,你可以做任何你想做的事

开始活动时,请执行以下操作:

am = (AlarmManager) getSystemService(ALARM_SERVICE);
timeoutInMillis = 60000;
Intent timeoutIntent = <create the intent you want to run here>;
timeoutPendingIntent = PendingIntent.getService(this, 0, timeoutIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(timeoutPendingIntent);
退出活动时,不要忘记清除警报:

am = (AlarmManager) getSystemService(ALARM_SERVICE);
timeoutInMillis = 60000;
Intent timeoutIntent = <create the intent you want to run here>;
timeoutPendingIntent = PendingIntent.getService(this, 0, timeoutIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(timeoutPendingIntent);
你可以用

  • 为收听加速计提供服务
  • AlarmManager在指定的时间后调用处理程序

在服务中,您收听来自加速计的事件,在每个事件中,您取消以前的报警并设置新的。达到一定时间后,您将从alarm中获得意图。

在alarmmanager类出现一些小问题后,我尝试了这种方法,没有这些问题,它会简单得多,干杯,伙计!感谢您的回答getekha,非常有用,我只是在不再需要报警管理器并且正在注销我的侦听器时遇到了一些问题,1个报警始终保持设置,即使在同一个PendingEvent上使用am.cancel,因此使用了DKITandroid的方法。为代码示例投票!