Created
March 26, 2011 10:49
-
-
Save amay077/888196 to your computer and use it in GitHub Desktop.
[Android]ZippyAsyncTask:自動的に少し待ってから ProgressDialog を表示する AsyncTask
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 com.amay077.android.os; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.DialogInterface.OnCancelListener; | |
import android.os.AsyncTask; | |
import android.os.Handler; | |
/*** | |
* プログレスダイアログを自動的に表示する AsyncTask の拡張です。 | |
* | |
* @author amay077 | |
*/ | |
public abstract class ZippyAsyncTask<Params, Progress, Result> | |
extends AsyncTask<Params, Progress, Result> { | |
private ProgressDialog dlg = null; | |
private Timer startTimer = new Timer(); | |
private Context context = null; | |
private String progressMessage = "now progress..."; | |
private int progressDelay = 200; | |
private boolean cancelable = true; | |
/** プログレスダイアログが表示されるまでの時間(ms)を取得 */ | |
public int getProgressDelay() { | |
return progressDelay; | |
} | |
/** プログレスダイアログが表示されるまでの時間(ms)を設定 */ | |
public void setProgressDelay(int progressDelay) { | |
this.progressDelay = progressDelay; | |
} | |
public ZippyAsyncTask(Context context) { | |
this.context = context; | |
} | |
/** プログレスダイアログのメッセージを設定 */ | |
public ZippyAsyncTask<Params, Progress, Result> setProgressMessage(String progressMessage) { | |
this.progressMessage = progressMessage; | |
return this; | |
} | |
/** プログレスダイアログのメッセージを取得 */ | |
public String getProgressMessage() { | |
return progressMessage; | |
} | |
/** 処理を中断できるかを設定 */ | |
public ZippyAsyncTask<Params, Progress, Result> setCancelable(boolean cancelable) { | |
this.cancelable = cancelable; | |
return this; | |
} | |
/** 処理を中断できるかを取得(既定値=true) */ | |
public boolean getCancelable() { | |
return this.cancelable; | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
final Handler handler = new Handler(); | |
startTimer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
openProgressDialog(); | |
} | |
}); | |
} | |
}, progressDelay); | |
} | |
@Override | |
protected void onCancelled() { | |
super.onCancelled(); | |
closeProgressDialog(); | |
stopTimer(); | |
} | |
@Override | |
protected void onPostExecute(Result result) { | |
super.onPostExecute(result); | |
closeProgressDialog(); | |
stopTimer(); | |
} | |
/** プログレスダイアログを表示する */ | |
protected void openProgressDialog() { | |
dlg = new ProgressDialog(context); | |
dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER); | |
dlg.setMessage(progressMessage); | |
if (cancelable) { | |
dlg.setOnCancelListener(new OnCancelListener() { | |
@Override | |
public void onCancel(DialogInterface dialog) { | |
ZippyAsyncTask.this.cancel(true); | |
} | |
}); | |
} else { | |
dlg.setCancelable(false); | |
} | |
dlg.show(); | |
} | |
/** プログレスダイアログを閉じる */ | |
protected void closeProgressDialog() { | |
if (dlg != null && dlg.isShowing()) { | |
dlg.dismiss(); | |
dlg = null; | |
} | |
} | |
/** プログレスダイアログ表示までのタイマーを止める */ | |
protected void stopTimer() { | |
if (startTimer != null) { | |
startTimer.cancel(); | |
startTimer.purge(); | |
startTimer = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e38b52:cancelable = true でも ProgressDialog が Back キーで閉じてしまうバグ修正