Исходники и картинки для статьи Android::Диалоговое окно ProgressDialog
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonShowProgress"
android:text="@string/showprogress"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonShowWheelDialog"
android:text="@string/showwheeldialog"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonThreadProgress"
android:text="@string/showprogressinthread"></Button>
</LinearLayout>
<resources>
<string
name="hello">Hello World, ProgressDialogDemoActivity!</string>
<string
name="app_name">ProgressDialogDemo</string>
<string
name="showprogress">Запустить горизонтальный индикатор прогресса</string>
<string
name="showwheeldialog">Показать круговой индикатор прогресса</string>
<string
name="showprogressinthread">Индикатор прогресса в другом потоке</string>
</resources>
package ru.alexanderklimov.progressdialogdemo;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ProgressDialogDemoActivity extends Activity
{
static final int IDD__HORIZONTAL_PROGRESS = 0;
static final int IDD_WHEEL_PROGRESS = 1;
static final int IDD_THREAD_PROGRESS = 2;
private ProgressDialog mProgressDialog;
private ProgressThread progressThread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button mButton = (Button)findViewById(R.id.buttonShowProgress);
final Button ButtonWheelProgress = (Button)findViewById(R.id.buttonShowWheelDialog);
final Button ButtonThreadProgress = (Button)findViewById(R.id.buttonThreadProgress);
mButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
// TODO Auto-generated method stub
showDialog(IDD__HORIZONTAL_PROGRESS);
}
});
ButtonWheelProgress.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
showDialog(IDD_WHEEL_PROGRESS);
}
});
ButtonThreadProgress.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
showDialog(IDD_THREAD_PROGRESS);
}
});
}
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case IDD__HORIZONTAL_PROGRESS:
mProgressDialog = new ProgressDialog(
ProgressDialogDemoActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("Загружаю. Подождите...");
return mProgressDialog;
case IDD_WHEEL_PROGRESS:
mProgressDialog = new ProgressDialog(
ProgressDialogDemoActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Загружаю. Подождите...");
return mProgressDialog;
case IDD_THREAD_PROGRESS:
mProgressDialog = new ProgressDialog(
ProgressDialogDemoActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("Загружаю. Подождите...");
return mProgressDialog;
default:
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
switch(id)
{
case IDD_THREAD_PROGRESS:
mProgressDialog.setProgress(0);
progressThread = new ProgressThread(handler);
progressThread.start();
}
}
// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.arg1;
mProgressDialog.setProgress(total);
if (total >= 100){
dismissDialog(IDD_THREAD_PROGRESS);
progressThread.setState(ProgressThread.STATE_DONE);
}
}
};
/** Nested class that performs progress calculations (counting) */
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) {
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
msg.arg1 = total;
mHandler.sendMessage(msg);
total++;
}
}
/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
} // onCreate
} // class