프로그래밍/안드로이드

안드로이드 작업스케쥴링 sendMessageDelayed를 언제 쓰지

가카리 2012. 8. 22. 21:06
반응형

 

 

boolean sendMessageAtTime(Message msg, long uptimeMillis)

// 이 메소드는 부팅후 경과시간을 사용하여 지정할 수 있습니다

 

boolean sendMessageDelayed(Message msg, long delayMillis)

//지금 시간으로 경과한 시간으로 지정한다 단위는 둘다 밀리초

 

러너블도 다음메소드를 이용해서 지연시간을 둘 수 있습니다

boolean postAtTime(Runnable r, long uptimeMillis)

boolean postDelayed(Runnable r, long delayMillis)

 

 

일단 이런것을 왜 쓰는 건지 이해하기위해서 다음 예제를 실행해봅시다.

 

일단은 delayed메시지를 안썼을때입니다.

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> 

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="업로드를 시작하려면 다음 버튼을 누르세요."

/>

<Button

       android:id="@+id/upload"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Upload"

/>

</LinearLayout>

 

자바파일

 

package com.android.ex94;

 

import android.app.*;

import android.content.*;

import android.os.*;

import android.view.*;

import android.widget.*;

 

public class ex94 extends Activity {

       public void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

       }

 

       public void mOnClick(View v) {

             new AlertDialog.Builder(this)

             .setTitle("질문")

             .setMessage("업로드 하시겠습니까?")

             .setPositiveButton("", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {

                           doUpload();

                    }

             })

             .setNegativeButton("아니오", null)

             .show();

       }

 

       void doUpload() {

             for (int i = 0; i < 20; i++) {

                    try { Thread.sleep(100); } catch (InterruptedException e) {;}

             }

             Toast.makeText(this, "업로드를 완료했습니다.", 0).show();

       }

}

 

 

 

 

 

 

실행해 보신분은 뭔가 이상함을 느낄 것입니다. 왜 예를 눌렀는데 잠시 멈춰있지? 렉걸린거 아냐? 이런 생각도 들겁니다. 그래서 이렇게 시간이 걸리는 것은 쓰레드로 처리를 해야합니다.

 

다음처럼 해봅시다.

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> 

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="업로드를 시작하려면 다음 버튼을 누르세요."

/>

<Button

     android:id="@+id/upload"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Upload"

/>

</LinearLayout>

 

 

자바파일

 

package com.android.ex95;

 

import android.app.*;

import android.content.*;

import android.os.*;

import android.view.*;

import android.widget.*;

 

public class ex95 extends Activity {

     public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.main);

     }

 

     public void mOnClick(View v) {

           new AlertDialog.Builder(this)

           .setTitle("질문")

           .setMessage("업로드 하시겠습니까?")

           .setPositiveButton("", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {

                     mHandler.sendEmptyMessageDelayed(0,10);

                     //0.01후에 메시지를 보내기로 하면

                     //mOnClick 리턴할 시간을 수있습니다.

                     // 대화상자가 닫힘니다.

                }

           })

           .setNegativeButton("아니오", null)

           .show();

     }

 

     Handler mHandler = new Handler() {

           public void handleMessage(Message msg) {

                if (msg.what == 0) {

                     doUpload();

                }

           }

     };

 

     void doUpload() {

           for (int i = 0; i < 20; i++) {

                try { Thread.sleep(100); } catch (InterruptedException e) {;}

           }

           Toast.makeText(this, "업로드를 완료했습니다.", 0).show();

     }

}

 

 

 

이번에는 바로 대화상자가 닫히고 잠시 뒤에 토스트메시지가 나오는 것을 알 수 있습니다.

왜 쓰는지 아셨나요?

반응형