¾È±Ô °øºÎ¹æ

Android > AsyncTask ¶õ ¹«¾ùÀΰ¡?

µî·ÏÀÏ : 2017-07-06 17:23 Á¶È¸¼ö : 53,015

AsyncTask´Â handler¿Í thread »ç¿ëÀ» Æí¸®ÇÏ°Ô »ç¿ëÇϱâ À§ÇØ ¸¸µé¾îÁø Ŭ·¡½ºÀÌ´Ù.

ÇÑ Å¬·¡½º ¾È¿¡¼­ UiÀÛ¾÷°ú ºñÁî´Ï½º ·ÎÁ÷ 󸮰¡ ¸ðµÎ °¡´ÉÇϱ⠶§¹®¿¡ °£ÆíÇÏ´Ù.

ÇÏÁö¸¸ ÇϳªÀÇ °´Ã¼·Î Àç»ç¿ëÀÌ ºÒ°¡´ÉÇÑ ¾öû³­ ´ÜÁ¡ÀÌ ÀÖÁö¸¸

°´Ã¼¸¦ »õ·Ó°Ô »ý¼ºÇؼ­ »ç¿ëÇÏ¸é µÈ´Ù. ÇÏÁö¸¸ ¸Þ¸ð¸® È¿À²ÀÌ ±×¸¸Å­ ³ªºü Áú ¼ö ÀÖ´Ù´ÂÁ¡.

»ç¿ëÇÏ´Â ¹æ¹ýÀº ¾Æ·¡ Äڵ带 º¸¸é ´ëÃæ ÀÌÇØ°¡ °¥°ÍÀÌ´Ù.

»ç¿ë¿¹´Â ¾öû³ª°Ô ¸¹ÀÌ ¾²ÀδÙ. ´Ù¿î·Îµå³ª ·Î±×ÀÎ, µîµî UI¿Í ·ÎÁ÷ÀÌ °°ÀÌ ½ÇÇàµÇ¾ß µÇ´Â

ºÎºÐÀº °ÅÀÇ ¸ðµÎ AsyncTask¸¦ »ç¿ëÇÑ´Ù°í º¸¸é µÈ´Ù.

AsyncTask´Â ³»ºÎ Ŭ·¡½º¸¦ »ç¿ëÇؼ­ ±¸ÇöÇؾߵȴÙ. ±¸±Û ·¹ÆÛ·±½º ¹®¼­¿¡µµ ±×·¸°Ô ³ª¿ÍÀÖ´Ù.

³»°¡ »ý°¢µÇ´Â ÀÌÀ¯´Â °£´ÜÇÏ´Ù. ³»ºÎŬ·¡½º·Î ±¸ÇöÇÔÀ¸·Î½á ¸Þ¸ð¸®¸¦ Àý¾àÇÒ ¼ö ÀÖ°í, Àç»ç¿ëÀ» À§Çؼ­

°´Ã¼¸¦ »ý¼ºÇؼ­ »ç¿ëÇϱâ À§Çؼ­ ÀÌ´Ù.
 

		public void asyncTaskCall() {
			new TestAsyncTask().execute();
		}
		 
		private class TestAsyncTask extends AsyncTask    
			   // doInBackground ¸Þ¼Òµå°¡ ½ÇÇàµÇ±â Àü¿¡ ½ÇÇàµÇ´Â ¸Þ¼Òµå
			@Override
			protected void onPreExecute() {
				// UI ÀÛ¾÷À» ¼öÇàÇÏ´Â ºÎºÐ
				super.onPreExecute();
			}
		 
			// ½ÇÁ¦ ºñÁî´Ï½º ·ÎÁ÷ÀÌ Ã³¸®µÉ ¸Þ¼Òµå(Thread ºÎºÐÀ̶ó°í »ý°¢ÇÏ¸é µÊ)
			@Override
			protected Void doInBackground(Void... params) {
				// onProgressUpdate¿¡¼­ 0À̶ó´Â °ªÀ» ¹Þ¾Æ¼­ ó¸®
				publishProgress(0);
				return null;
			}
		 
			// doInBackground¿¡¼­ ³Ñ±ä values °ªÀ» ¹Þ¾Æ¼­ ó¸®ÇÏ´Â ºÎºÐ
			@Override
			protected void onProgressUpdate(Integer... values) {
				switch(values[0]) {
				case 0:
					// doInBackground¿¡¼­ publicshProgress·Î ³Ñ±ä°ªÀ» ó¸®ÇÏ´Â ºÎºÐ
					break;
				}
				super.onProgressUpdate(values);
			}
		 
			// ¸ðµç ÀÛ¾÷ÀÌ ³¡³­ ÈÄ Ã³¸®µÇ´Â ¸Þ¼Òµå
			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
			}
		 
		}

 
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.