はい、AsyncTaskで画像をダウンロードする必要があります(URLからダウンロードしていると想定しています)。機能を効果的に実現するには、次のことを行う必要があります。
- AsyncTaskを作成してイメージをダウンロードし(doInBackground()でダウンロードを実装)、postExecute()でイメージが正常にダウンロードされたかどうかを追跡するブール値(isImageDownloadedなど)も用意します。ダウンロードを開始する前に、プログレスバーも表示することを忘れないでください
- AsyncTaskを実行してダウンロードを開始します
- android.os.CountDownTimerの拡張子を作成して30秒カウントダウンします
- onFinish()メソッドで、追跡するブール値を確認します。falseの場合は、AsyncTaskをキャンセルして、意図したトースト/ダイアログをスローします。
以下は、上記の手順の擬似コード/スケルトンです(構文をチェックしなかったため、エラーが発生したことをお詫びします)
public void downloadAndCheck() {
AsyncTask downloadImageAsyncTask =
new AsyncTask() {
@Override
protected Boolean doInBackground(Void... params) {
// download image here, indicate success in the return boolean
}
@Override
protected void onPostExecute(Boolean isConnected) {
// set the boolean result in a variable
// remove the progress bar
}
};
try {
downloadImageAsyncTask.execute();
} catch(RejectedExecutionException e) {
// might happen, in this case, you need to also throw the alert
// because the download might fail
}
// note that you could also use other timer related class in Android aside from this CountDownTimer, I prefer this class because I could do something on every interval basis
// tick every 10 secs (or what you think is necessary)
CountDownTimer timer = new CountDownTimer(30000, 10000) {
@Override
public void onFinish() {
// check the boolean, if it is false, throw toast/dialog
}
@Override
public void onTick(long millisUntilFinished) {
// you could alternatively update anything you want every tick of the interval that you specified
}
};
timer.start()
}