public class AsyncTaskDemo2 extends Activity implements View.OnClickListener
{
ProgressBar progressBar;
TextView textResult;
Button btnExecuteTask;
private static boolean mAsyncTaskExecute = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textResult = (TextView) findViewById(R.id.textResult);
btnExecuteTask = (Button) findViewById(R.id.btnExecuteTask);
btnExecuteTask.setOnClickListener(this);
}
public void onClick(View v)
{
try
{
WaitNotify waitNotify = new WaitNotify();
DoComplecatedJob asyncTask1 = new DoComplecatedJob(waitNotify);
asyncTask1.execute("AsyncTask1");
DoComplecatedJob asyncTask2 = new DoComplecatedJob(waitNotify);
asyncTask2.execute("AsyncTask2");
}
catch(Exception e)
{
Log.i("Debug", e.getMessage());
}
}
private class WaitNotify{
synchronized public void mWait()
{
try
{
wait();
}
catch(Exception e)
{
Log.i("Debug", e.getMessage());
}
}
synchronized public void mNotify()
{
try
{
notify();
}
catch(Exception e)
{
Log.i("Debug", e.getMessage());
}
}
}
// AsyncTask클래스는 항상 Subclassing 해서 사용 해야 함.
// 사용 자료형은
// background 작업에 사용할 data의 자료형: String 형
// background 작업 진행 표시를 위해 사용할 인자: Integer형
// background 작업의 결과를 표현할 자료형: Long
private class DoComplecatedJob extends AsyncTask<String, Integer, Integer>
{
private String mTitle = null;
private WaitNotify mWaitNotify = null;
public DoComplecatedJob(WaitNotify aWaitNotify)
{
mWaitNotify = aWaitNotify;
}
// UI 스레드에서 AsynchTask객체.execute(...) 명령으로 실행되는 callback
@Override
protected Integer doInBackground(String... strData)
{
if(mAsyncTaskExecute)
{
mWaitNotify.mWait();
}
mAsyncTaskExecute = true;
mTitle = strData[0];
int cnt = 0;
while (true)
{
if(cnt>5)
{
break;
}
else
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
Log.i("Debug", e.getMessage());
}
publishProgress(cnt++);
}
}
return 0;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
Log.i("Debug", "[" + mTitle + "]" + ": " + progress[0]);
}
@Override
protected void onPostExecute(Integer result) {
Log.i("Debug", "onPostExecute");
mWaitNotify.mNotify();
mAsyncTaskExecute = false;
}
}
}