Created
September 9, 2010 02:15
-
-
Save skatldjs/571246 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exam.androidtest; | |
import android.app.Activity; | |
import android.app.Dialog; | |
import android.app.ProgressDialog; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
public class NotificationTest extends Activity { | |
static final int PROGRESS_DIALOG = 0; | |
Button button; | |
ProgressThread progressThread; | |
ProgressDialog progressDialog; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
button = (Button) findViewById(R.id.btn); | |
button.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
showDialog(PROGRESS_DIALOG); | |
} | |
}); | |
} | |
protected Dialog onCreateDialog(int id) { | |
switch (id) { | |
case PROGRESS_DIALOG: | |
progressDialog = new ProgressDialog(NotificationTest.this); | |
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); | |
progressDialog.setMessage("Loading..."); | |
progressThread = new ProgressThread(handler); | |
progressThread.start(); | |
return progressDialog; | |
default: | |
return null; | |
} | |
} | |
// 핸들러는 정의하여 스레드가 메시지를 보내면 프로그레스를 업데이트 합니다. | |
final Handler handler = new Handler() { | |
public void handleMessage(Message msg) { | |
int total = msg.getData().getInt("total"); | |
progressDialog.setProgress(total); | |
if (total >= 100) { | |
dismissDialog(PROGRESS_DIALOG); | |
progressThread.setState(ProgressThread.STATE_DONE); | |
} | |
} | |
}; | |
/** 프로그레스를 처리하는 클래스를 내부 클래스로 정의. */ | |
private class ProgressThread extends Thread { | |
Handler mHandler; | |
final static int STATE_DONE = 0; | |
final static int STATE_RUNNING = 1; | |
int mState; | |
int total; | |
ProgressThread(Handler h) { | |
mHandler = h; | |
} | |
public void run() { | |
mState = STATE_RUNNING; | |
total = 0; | |
while (mState == STATE_RUNNING) { | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
// 에러처리 | |
} | |
Message msg = mHandler.obtainMessage(); | |
Bundle b = new Bundle(); | |
b.putInt("total", total); | |
msg.setData(b); | |
mHandler.sendMessage(msg); | |
total++; | |
} | |
} | |
// 현재의 상태를 설정하는 메소드 | |
public void setState(int state) { | |
mState = state; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment