android 中native层使用线程实现定时器功能

在构造函数或init的什么位置创建线程

pthread_attr_t attr;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&mThread, &attr, ThreadWrapper, this);
pthread_attr_destroy(&attr);

//记录第一个文件时间 time_t mStartRecordTime;

time(&mStartRecordTime); 

mStartRecordTime -=1; // 第一次文件差一秒


mStopTimerFlag = false; //线程停止标志


线程实体:

void *StagefrightRecorder::ThreadWrapper(void *me) {
    StagefrightRecorder *recorder = static_cast(me);
    recorder->threadFunc();

    return NULL;
}
void StagefrightRecorder::threadFunc() {
prctl(PR_SET_NAME, (unsigned long)"StagefrightRecorder_timer", 0, 0, 0);

//androidSetThreadPriority(0, ANDROID_PRIORITY_HIGHEST);
Mutex::Autolock autoLock(mLock);
while (!mStopTimerFlag) {

time_t timer; 
time(&timer); 
struct tm* tm_t; 
tm_t = localtime(&timer); 

if((timer - mStartRecordTime) >= 10/*60*10*/){
ALOGE("!!!  222 StagefrightRecorder timer thread is ryning  now time hour=%d, min=%d, second=%d ,now timer=%d, mStartRecordTime=%d  !!!", tm_t->tm_hour, tm_t->tm_min, tm_t->tm_sec, timer, mStartRecordTime);

// tm_t->tm_year+1900, tm_t->tm_mon+1, tm_t->tm_mday, tm_t->tm_hour, tm_t->tm_min, tm_t->tm_sec);

//定时器需要完成的功能  10秒钟的定时器

。。。

//重新计时
mStartRecordTime = timer;
}

//延时1秒
//sleep(1); //second
usleep(1000);// milliseconds
}
}

你可能感兴趣的:(java,C++,android)