노무현 대통령 배너

Double-J's World

blog logo image

Double-J's World » Search » Results » Articles

dialog와 관련된 글 1개

  1. 2009.10.31 Example ProgressDialog with a second thread / Double-J

Double-J's World » Programming/Android

Example ProgressDialog with a second thread

Double-J | 2009. 10. 31. 11:19

진행 상태를 추적하기 위해 두 번재 thread 를 사용한다.

그 thread 는 진행이 이루어질 때마다 핸들러를 통해서 main activity  에게 message 를 보낸다.

메시지를 받은 activity 는 Progress Dialog 를 update 한다.



01package com.example.progressdialog;
02 
03import android.app.Activity;
04import android.app.Dialog;
05import android.app.ProgressDialog;
06import android.os.Bundle;
07import android.os.Handler;
08import android.os.Message;
09import android.view.View;
10import android.view.View.OnClickListener;
11import android.widget.Button;
12 
13public class NotificationTest extends Activity {
14    static final int PROGRESS_DIALOG = 0;
15    Button button;
16    ProgressThread progressThread;
17    ProgressDialog progressDialog;
18    
19    /** Called when the activity is first created. */
20    public void onCreate(Bundle savedInstanceState) {
21        super.onCreate(savedInstanceState);
22        setContentView(R.layout.main);
23 
24        // Setup the button that starts the progress dialog
25        button = (Button) findViewById(R.id.progressDialog);
26        button.setOnClickListener(new OnClickListener(){
27            public void onClick(View v) {
28                showDialog(PROGRESS_DIALOG);
29            }
30        });
31    }
32    
33    protected Dialog onCreateDialog(int id) {
34        switch(id) {
35        case PROGRESS_DIALOG:
36            progressDialog = new ProgressDialog(NotificationTest.this);
37            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
38            progressDialog.setMessage("Loading...");
39            progressThread = new ProgressThread(handler);
40            progressThread.start();
41            return progressDialog;
42        default:
43            return null;
44        }
45    }
46 
47    // Define the Handler that receives messages from the thread and update the progress
48    final Handler handler = new Handler() {
49        public void handleMessage(Message msg) {
50            int total = msg.getData().getInt("total");
51            progressDialog.setProgress(total);
52            if (total >= 100){
53                dismissDialog(PROGRESS_DIALOG);
54                progressThread.setState(ProgressThread.STATE_DONE);
55            }
56        }
57    };
58 
59    /** Nested class that performs progress calculations (counting) */
60    private class ProgressThread extends Thread {
61        Handler mHandler;
62        final static int STATE_DONE = 0;
63        final static int STATE_RUNNING = 1;
64        int mState;
65        int total;
66        
67        ProgressThread(Handler h) {
68            mHandler = h;
69        }
70        
71        public void run() {
72            mState = STATE_RUNNING;  
73            total = 0;
74            while (mState == STATE_RUNNING) {
75                try {
76                    Thread.sleep(100);
77                } catch (InterruptedException e) {
78                    Log.e("ERROR", "Thread Interrupted");
79                }
80                Message msg = mHandler.obtainMessage();
81                Bundle b = new Bundle();
82                b.putInt("total", total);
83                msg.setData(b);
84                mHandler.sendMessage(msg);
85                total++;
86            }
87        }
88         
89        /* sets the current state for the thread,
90         * used to stop the thread */
91        public void setState(int state) {
92            mState = state;
93        }
94    }
95}
출처 : http://developer.android.com/guide/topics/ui/dialogs.html


(go to top)