프로그래밍/안드로이드

안드로이드 작업스케쥴링 두번째 postDelayed 메소드를 써보자.

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

작업스케쥴링 두번째 postDelayed 메소드를 써보자

 

첫번째 예제를 단순히 postDelayed로 바꿔본 것 입니다.

 

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.ex96;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.os.Handler;

import android.view.View;

import android.widget.Toast;

 

public class ex96 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.postDelayed(new Runnable() {//이번에는 sendDelayedMessage메소드가 아닌

                                 //posetDelayed메소드로 바꿔보았습니다.

                                 public void run() {

                                        doUpload();

                                 }

                           },10);//두번째 인자는 10ms겠죠?

                    }

             })

             .setNegativeButton("아니오", null)

             .show();

       }

 

       Handler mHandler = new Handler();//첫번째에서는 handleMessage메소드를 구현했구요

       //지금은 아니죠

 

       void doUpload() {

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

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

             }

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

       }

}

 

 

반응형