A. 如何在android下採用相對時間,實現超時等待的功能
一、函數功能說明
pthread_cond_timedwait 等待一個條件變數,或者超時就會返回
POSIX有兩種時鍾類型
1、CLOCK_REALTIME: 系統范圍內的實時時鍾,是個軟體時鍾,可以通過命令等方式修改該系統時間.
2、CLOCK_MONOTONIC:系統起機時到現在的時間,不能被設置和修改.
pthread_cond_timedwait()在沒有設置條件變數屬性的時候,默認用的是CLOCK_REALTIME軟體時間,
因此在極端情況下會出現實際等待的時間與設置的超時時間不同。
所以,對於linux的超時等待功能,最好是使用CLOCK_MONOTONIC進行實現,並且通過pthread_condattr_setclock實現。
而對於android系統而言,是不支持pthread_condattr_setclock,通過驗證可以採用函數pthread_cond_timedwait_monotonic實現。
下面直接給出代碼的實現功能。
二、超時等待功能
[cpp] view plain
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <time.h>
static pthread_mutex_t s_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER;
void PthreadAttr_Init(void);
unsigned long long getSysTime(void);
void waitTimeout(void);
void PthreadAttr_Init(void)
{
#if defined(ANDROID)
#else
pthread_condattr_t cattr;
int iRet = -1;
iRet = pthread_condattr_init(&cattr);
if (iRet != 0)
{
return;
}
pthread_mutex_init(&s_mut, NULL);
pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
pthread_cond_init(&s_cond, &cattr);
pthread_condattr_destroy(&cattr);
#endif
return;
}
void waitTimeout(void)
{
unsigned long long ullbefore = getSysTime();
unsigned long long ullafter = 0;
#if defined(ANDROID)
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) // 支持ANDROID下NDK的編譯,採用相對時間
struct timespec outtime;
memset(&outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, &outtime);
outtime.tv_sec += 2;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait_monotonic(&s_cond,&s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####01 interval[%lld] ms\n", ullafter - ullbefore);
#else //支持ANDROID下NDK的編譯,採用絕對時間
struct timeval now;
struct itmespec outtime;
gettimeofday(&now, NULL);
outtime.tv_sec = now..tv_sec + 3;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait(&s_cond, &s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####02 interval[%lld] ms\n", ullafter - ullbefore);
#endif
#else // 支持LINUX下的編譯,採用絕對時間
struct timespec outtime;
memset(&outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, &outtime);
outtime.tv_sec += 4;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait(&s_cond, &s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####03 interval[%lld] ms\n", ullafter - ullbefore);
#endif
return;
}
unsigned long long getSysTime(void)
{
unsigned long long milliseconds = 0;
struct tms t_tmsTime;
clock_t t_CurTime;
static int s_clks_per_sec = 0;
if (s_clks_per_sec == 0)
{
s_clks_per_sec = sysconf(_SC_CLK_TCK);
}
if (s_clks_per_sec == 0)
{
return 0;
}
t_CurTime = times(&t_tmsTime);
if (1000 % s_clks_per_sec == 0)
{
milliseconds = (1000 /s_clks_per_sec)*(unsigned long long )t_CurTime;//換算成毫秒
}
else
{
milliseconds = 1000 * (unsigned long long )t_CurTime/s_clks_per_sec;//換算成毫秒
}
return milliseconds;
}
int main(void)
{
PthreadAttr_Init();
waitTimeout();
return 0;
}
編譯命令:
gcc test_ptthrad_conf_timewait_monotonic.c -o test_ptthrad_conf_timewait_monotonic -lpthread -lrt
linux下的測試結果:
####03 interval[4010] ms
B. Android如何計算時間差
你先獲取一個時間system.cur..獲取時間然後保存到一個long里邊在第二次獲取的時候就可以減去第一個時間了。尤其在判斷點擊兩次退出程序的時候很管用的。
C. android studio怎麼顯示方法耗時
你在方法開始和結束時都獲得一次當前時間 兩者相減就是方法所耗時間了
D. Android開發秒錶如何避免時間差
Android開發秒錶避免時間差:
可以利用系統時間,首先秒錶開始的時候獲取一個系統時間作為初始時間。然後設置一個Timer來定時獲取現在距離初始時間的時間差,再賦值給秒錶時間,這樣秒錶時間與系統時間就可以實現同步了。誤差會有,但是用戶已經不會感覺出來了。
E. android 計算時間多少分鍾前
你這問題的核心就是計算時間差啊,網上代碼很多的。
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date d1 = df.parse("2006-05-26 12:00:00");
Date d2 = df.parse("2006-07-02 11:20:00");
//Date d2 = new Date(System.currentTimeMillis());//你也可以獲取當前時間
long diff = d1.getTime() - d2.getTime();//這樣得到的差值是微秒級別
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
System.out.println(""+days+"天"+hours+"小時"+minutes+"分");
}
catch (Exception e)
{
}
F. 怎麼實現系統時間相減得到分鍾 安卓
try {
java.util.Date nowdate = new Date();
java.util.Date setdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2014-05-28 18:30:59");
long between = (setdate.getTime() - nowdate.getTime());
boolean result = between < 1000 * 60 * 60;
System.out.println(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public boolean isOneHourPassed() {
boolean result=false;
try {
java.util.Date nowdate = new Date();
String schDate = date+" "+timeSet ? time : "23:59";
java.util.Date setdate = new SimpleDateFormat("yyyy-MM-dd HH:mm")
.parse(schDate);
long between = (setdate.getTime() - nowdate.getTime());
result = between < 1000 * 60 * 60;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
G. android中如何比較兩個時間的先後
首先確定這兩個時間是同一個時區的(這樣才有可比性)。建議使用UTC零時區時間。
然後通過SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
把時間格式轉化為年月日時分秒的一串數字。
把得到的String強制轉化為int進行比較。
我也沒做過,不過希望能給你一個參考方案。
H. android 如何在ACTION_MOVE算出手指在屏幕上拖動的距離,和拖動的時間
switch (action) {
case MotionEvent.ACTION_DOWN:
DownX = event.getX();//float DownX
DownY = event.getY();//float DownY
currentMS = System.currentTimeMillis();//long currentMS 獲取系統時間
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getX() - DownX;//X軸距離
float moveY = event.getY() - DownY;//y軸距離
long moveTime = System.currentTimeMillis() - currentMS;//移動時間
break;
case MotionEvent.ACTION_UP:
break;
}
I. 問個關於安卓開發中的時間相減的問題
try {
java.util.Date nowdate = new Date();
java.util.Date setdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2014-05-28 18:30:59");
long between = (setdate.getTime() - nowdate.getTime());
boolean result = between < 1000 * 60 * 60;
System.out.println(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}