ÁÖ±âÀûÀÎ ¹Ýº¹ÀÛ¾÷À¸·Î TimerTask ³ª CountDownÀ» ¾²´Â ¹æ¹ýµµ ÀÖ½À´Ï´Ù.
Àúµµ À§ÀÇ ¹æ¹ýÀ» »ç¿ëÇÏ´Ù°¡ À§ µÎ ¹æ¹ýÀÇ °øÅëÀûÀÎ ´ÜÁ¡Àº, ÁÖ±âÀÇ ½Ã°£À» ¸¶À½´ë·Î º¯°æÇÒ ¼ö
¾ø´Ù´Â Á¡ÀÔ´Ï´Ù.
TimerTask´Â timerÀÇ ½ºÄÉÁì¿¡ ÀÇÇØ ÀÏÁ¤ÇÑ ½Ã°£À¸·Î ¹Ýº¹µÇ°í,
CountDown ¿ª½Ã ÃʱⰪ¿¡ ÀÇÇØ ½Ã°£ÀÌ ´Ù µÉ ¶§±îÁö ¹Ýº¹ÀÛ¾÷À» ÇÕ´Ï´Ù.
TimerTask´Â »ç¿ëÀÚ°¡ ½ºÄÉÁìÀ» cancelÇÏÁö ¾Ê´Â ÀÌ»ó ²¨ÁöÁö ¾Ê´Â ¹«ÇÑ ¹Ýº¹ÀÌ°í,
CountDownÀº ¹Ý´ëÀÇ °æ¿ìÁÒ.
°£´ÜÇÑ ¹Ýº¹ ÀÛ¾÷¿¡´Â ¹«¸®°¡ ¾øÀ» Áö ¸ð¸£³ª, ³×ÀÌÆ®¿Â ó·³ ½Ã°£À» Á¡Á¡ ´Ã·Á°¡¸ç ÁÖ±â½Ã°£À» ´Ã¸±
¼ö´Â ¾ø¾î¼ HandlerÀÇ sendEmptyMessageDelayed¸¦ »ç¿ëÇß½À´Ï´Ù.
¿ø¸®´Â Á¤ÀÇÇÑ xÃʸ¸ÅÀÇ ½Ã°£ µÚ¿¡ handler¿¡°Ô ¸Þ¼¼Áö¸¦ ½î°í handler´Â ´Ù½Ã ÇØ´ç method¸¦
ºÎ¸¨´Ï´Ù.
ÀÌ·±½ÄÀ¸·Î ÇÏ¸é »ç¿ëÀÚ°¡ ÁßÁöÇÏ°í ½ÍÀº ½Ã°£À» ¼³Á¤ÇÒ ¼öµµ ÀÖ°í, ¸Þ¼¼Áö¸¦ ¹ÞÀ»¶§¸¶´Ù °¡º¯ÀûÀ¸·Î
½Ã°£À» Á¶Á¤ÇÏ¿© ½ÃÀÛ½Ãų¼öµµ ÀÖÁÒ.
¿ì¼± handlerÂÊÀ» º¾½Ã´Ù.
long reconnectionTime = 1000;
long limitTime = 3600000;
Handler connectionHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
if(reconnectionTime <= ( limitTime /2))
{
reconnectionTime = reconnectionTime*2;
Log.d("connectionRetry", "reconnectionTime : "+reconnectionTime);
try{
handlerconnect();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
else
{
reconnectionTime = limitTime ;
Log.d("connectionRetry", "reconnectionTime : "+reconnectionTime);
try{
handlerconnect();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
break;
case 1:
Log.d("connectionRetry", "Login_success_full...handler work end....");
break;
}
super.handleMessage(msg);
}
};
handler¿¡¼´Â 0À» ¹ÞÀ»¶§¸¶´Ù ¸Þ¼¼Áö µô·¹À̽ð£À» 2¹è¾¿ ´Ã¸®°í, ÇÑ°è½Ã°£ÀÎ 10ºÐ¿¡ µµ´ÞÇϸé
´õÀÌ»ó Áõ°¡ÇÏÁö ¾Ê°í 10ºÐ¸¶´Ù Çѹø¾¿ ¸Þ¼¼Áö¸¦ ½õ´Ï´Ù.
´ÙÀ½Àº handlerconnect(); ÀÔ´Ï´Ù.
public void hanadlerconnect()
{
int rend=0;
Random rand = new Random();
rend = Math.abs(rand.nextInt()%9);
if(rend ==6)
{
connectionHandler.sendEmptyMessageDelayed(1, 1000);
}else
{
connectionHandler.sendEmptyMessageDelayed(0, reconnectionTime);
}
}
rend °ªÀÌ 6ÀÌ¸é ¹Ýº¹ÀÛ¾÷À» ¸ØÃß°í, Á¾·áÇÕ´Ï´Ù.
rend °ªÀÌ 6ÀÌ ¾Æ´Ï¸é µô·¹ÀÌ°ªÀ» Çڵ鷯¿¡ ÀÇÇØ 2¹è¾¿ ´Ã·Á°¡¸ç ÀÛ¾÷À» ¹Ýº¹ÇÕ´Ï´Ù.
¹°·Ð ¾²·¹µå¸¦ »ç¿ëÇصµ °¡´ÉÇÕ´Ï´Ù. ´Ü, ¾²·¹µå »ç¿ë½Ã¿¡´Â ¹Ýµå½Ã ±× °´Ã¼°¡ Á¾·áµÇ°Å³ª
wait»óÅ À̾î¾ß °ÙÁÒ.
Ȥ½Ã¶óµµ °¡º¯½Ã°£À» °®´Â ¹Ýº¹ÀÛ¾÷ÀÌ ÇÊ¿äÇϽźеéÀº ½ºÄÉÁì·¯ ´ë½Å À̹æ¹ýÀ» ¾²´Â°Íµµ
ÁÁÀº ¹æ¹ýÀÏ °Ì´Ï´Ù.
* ±ÛÀ» µî·ÏÇϽǶ§ ²À ÇÊ¿äÇÑ °æ¿ì°¡ ¾Æ´Ï¸é °³ÀÎÁ¤º¸¸¦ ³²±âÁö ¸¶¼¼¿ä ^^ (¿¬¶ôó,À̸ÞÀÏÁÖ¼Ò,Áֹεî·Ï¹øÈ£ µî)