android超时,手机一直请求超时

如何在android下采用相对时间,实现超时等待的功能

一、函数功能说明

创新互联主要从事网站设计、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务武陵,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

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 copy

#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

如何修改android系统的“屏幕超时”

今天修改了android2.3的“屏幕超时”部分,主要是修改了超时时间,添加了“永不休眠”选项。这部分修改只是更改了对应的XML文件,没有涉及到代码修改,大家都知道xml文件的作用,这里就不多说了。需要修改的文件如下:

android2.3\packages\apps\Settings\res\values\arrays.xml 中:

!-- Display settings. The delay in inactivity before the screen is turned off. These are shown ain a list dialog. --

string-array name="screen_timeout_entries"

item15 seconds/item

item30 seconds/item

item1 minute/item

item10 minutes/item

item30 minutes/item

itemnever sleep/item

/string-array

!-- Do not translate. --

string-array name="screen_timeout_values" translatable="false"

!-- Do not translate. --

item15000/item

!-- Do not translate. --

item30000/item

!-- Do not translate. --

item60000/item

!-- Do not translate. --

item600000/item

!-- Do not translate. --

item1800000/item

!-- Do not translate. --

item-1/item

/string-array

要是是系统屏幕永不超时也就是将待机时间设为“-1”,这是最关键的一点;

对应英文版本:

android2.3\packages\apps\Settings\res\values-cs\arrays.xml

string-array name="screen_timeout_entries"

item msgid="3342301044271143016""15 sekund"/item

item msgid="8881760709354815449""30 sekund"/item

item msgid="7589406073232279088""1 minuta"/item

item msgid="7001195990902244174""10 minuty"/item

item msgid="5721688686241190620""30 minut"/item

item msgid="7156442995039264948""never sleep."/item

/string-array

对应中文版本:

android\android_gingerbread_skdv210\packages\apps\Settings\res\values-zh-rCN\arrays.xml

string-array name="screen_timeout_entries"

item msgid="3342301044271143016""15 秒"/item

item msgid="8881760709354815449""30 秒"/item

item msgid="7589406073232279088""1 分钟"/item

item msgid="7001195990902244174""10 分钟"/item

item msgid="5721688686241190620""30 分钟"/item

item msgid="7156442995039264948""从不休眠"/item

/string-array

android中如何获取超时时长的异常

android获取超时时长的异常方式如下:设置超时机制

client.getParams().setIntParameter(

HttpConnectionParams.SO_TIMEOUT, TIME_OUT_DELAY); // 超时设置

client.getParams().setIntParameter(

HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT_DELAY);// 连接超时

这里设置了两种超时,第一种是请求超时,第二种时连接超时。

当向服务器发出请求后,请求和服务器建立socket连接,但是很长时间内都没有建立socket连接,这就时第一种请求超时,这种情况主要发生在请求了

一个不存在的服务器。超时之后,会抛出InterruptedIOException异常。

Timeout for blocking operations. The argument value is specified in

milliseconds. An InterruptedIOException is thrown if this timeout

expires.


名称栏目:android超时,手机一直请求超时
文章来源:http://ybzwz.com/article/dsseeep.html